Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Tried to implement <strong>jfelectron</strong>'s solution because it is a "<em>rock-solid solution to these issues that conforms with the 'Android Way' of things</em>" but it took some time to look up and put together all the elements mentioned. Ended up with this slightly different, and I think more elegant, solution posted here in it's entirety. </p> <p>Uses an IntentService fired from an activity to perform the long running task on a separate thread. The service fires back sticky Broadcast Intents to the activity which update the dialog. The Activity uses showDialog(), onCreateDialog() and onPrepareDialog() to eliminate the need to have persistent data passed in the application object or the savedInstanceState bundle. This should work no matter how your application is interrupted.</p> <p>Activity Class:</p> <pre><code>public class TesterActivity extends Activity { private ProgressDialog mProgressDialog; private static final int PROGRESS_DIALOG = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button) this.findViewById(R.id.test_button); b.setOnClickListener(new OnClickListener() { public void onClick(View v) { buttonClick(); } }); } private void buttonClick(){ clearPriorBroadcast(); showDialog(PROGRESS_DIALOG); Intent svc = new Intent(this, MyService.class); startService(svc); } protected Dialog onCreateDialog(int id) { switch(id) { case PROGRESS_DIALOG: mProgressDialog = new ProgressDialog(TesterActivity.this); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setMax(MyService.MAX_COUNTER); mProgressDialog.setMessage("Processing..."); return mProgressDialog; default: return null; } } @Override protected void onPrepareDialog(int id, Dialog dialog) { switch(id) { case PROGRESS_DIALOG: // setup a broadcast receiver to receive update events from the long running process IntentFilter filter = new IntentFilter(); filter.addAction(MyService.BG_PROCESS_INTENT); registerReceiver(new MyBroadcastReceiver(), filter); break; } } public class MyBroadcastReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { if (intent.hasExtra(MyService.KEY_COUNTER)){ int count = intent.getIntExtra(MyService.KEY_COUNTER, 0); mProgressDialog.setProgress(count); if (count &gt;= MyService.MAX_COUNTER){ dismissDialog(PROGRESS_DIALOG); } } } } /* * Sticky broadcasts persist and any prior broadcast will trigger in the * broadcast receiver as soon as it is registered. * To clear any prior broadcast this code sends a blank broadcast to clear * the last sticky broadcast. * This broadcast has no extras it will be ignored in the broadcast receiver * setup in onPrepareDialog() */ private void clearPriorBroadcast(){ Intent broadcastIntent = new Intent(); broadcastIntent.setAction(MyService.BG_PROCESS_INTENT); sendStickyBroadcast(broadcastIntent); }} </code></pre> <p>IntentService Class:</p> <pre><code>public class MyService extends IntentService { public static final String BG_PROCESS_INTENT = "com.mindspiker.Tester.MyService.TEST"; public static final String KEY_COUNTER = "counter"; public static final int MAX_COUNTER = 100; public MyService() { super(""); } @Override protected void onHandleIntent(Intent intent) { for (int i = 0; i &lt;= MAX_COUNTER; i++) { Log.e("Service Example", " " + i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } Intent broadcastIntent = new Intent(); broadcastIntent.setAction(BG_PROCESS_INTENT); broadcastIntent.putExtra(KEY_COUNTER, i); sendStickyBroadcast(broadcastIntent); } }} </code></pre> <p>Manifest file entries:</p> <p>before application section:</p> <pre><code>uses-permission android:name="com.mindspiker.Tester.MyService.TEST" uses-permission android:name="android.permission.BROADCAST_STICKY" </code></pre> <p>inside application section</p> <pre><code>service android:name=".MyService" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload