How to implement a BroadcastReceiver in Android?

How to implement a BroadcastReceiver in Android?



package com.ClinicalInterface;

public class TestActivity extends ListActivity {

    static AsyncDataLoader mAsyncDataLoader;
    static ProgressDialog dialog;
    static ArrayList<String> list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dialog = new ProgressDialog(this);
        dialog.setMessage("Loading...");
        dialog.show();
        mAsyncDataLoader = new AsyncDataLoader();
        mAsyncDataLoader.execute(); 
     }

    public class AsyncDataLoader extends AsyncTask<Void, Integer, String> {       

        public class mTestReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent){
                System.out.println( "I've received something!" );
                publishProgress(2);
            }
        }

        @Override
        protected String doInBackground( Void ... params ) {           
            TestLoader tl = new TestLoader();
            tl.setContext(getApplicationContext());
            tl.setServeraddress("192.168.2.109");
            list = tl.doLST(null);
            return "COMPLETE!";
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            if (values[0] == 2) {
                dialog.setMessage("Loading data ...");
            }
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            dialog.dismiss();
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(TestActivity.this, R.layout.list_item, list);
            TestActivity.this.setListAdapter(adapter);                   
        }
    }       
}

package com.ClinicalInterface;

public class TestLoader {
    private Context mContext;

    public void setContext(Context context) {
        mContext = context;
    }

     public ArrayList<String> doLST(String arg) {

         // Send intent to AsyncTask       
         Intent intent = new Intent(mContext, mTestReceiver.class);
         intent.setAction("PROGRESS");
         mContext.sendBroadcast(intent);
         System.out.println( "message sent" );

         // Code that actually does network io removed for brevity
    }
}

<activity android:name="TestActivity" android:label="TestActivity">
    <receiver android:name=".AsyncDataLoader.mTestReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PROGRESS" />
        </intent-filter>
    </receiver>
</activity>

@Override
protected String doInBackground( Void ... params ) {           
    TestLoader tl = new TestLoader();
    IntentFilter intentFilter = new IntentFilter("PROGRESS");
    mReceiver mmReceiver = new mReceiver();
    registerReceiver(mmReceiver, intentFilter);           
    tl.setContext(getApplicationContext());
    tl.setServeraddress("192.168.2.109");
    list = tl.doLST(null);
    unregisterReceiver(mmReceiver);
    return "COMPLETE!";
}

Intent intent = new Intent();
 intent.setAction("PROGRESS");
 mContext.sendBroadcast(intent);

Comments

Popular posts from this blog

How to build unlimited levels menu through PHP and MySQL

How to build a map of the frequency with which characters occur in a file in OCaml?

How to create timed pop-up windows using MVVM?