1

I think the BroadcastReceiver AlarmManagerBroadcastReceiver will keep running even if I exit the app. I think the BroadcastReceiver will restore and keep running automatically when I reboot system. In order to stop the Broadcast receiver, I have to uninstall the app, right?

Layout

<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
    xmlns:tools='http://schemas.android.com/tools'
    android:layout_width='match_parent'
    android:layout_height='match_parent' 
    android:orientation='vertical'>

    <Button
        android:layout_width='fill_parent'
        android:layout_height='wrap_content'
        android:padding='@dimen/padding_medium'
        android:text='@string/start_repeating_alarm'
        android:onClick='startRepeatingAlarm'
        tools:context='.EnableDisableBroadcastReceiver' />

    <Button
       android:layout_width='fill_parent'
       android:layout_height='wrap_content'
       android:padding='@dimen/padding_medium'
       android:text='@string/cancel_alarm'
       android:onClick='cancelAlarm'
       tools:context='.EnableDisableBroadcastReceiver' />

    <Button
        android:layout_width='fill_parent'
        android:layout_height='wrap_content'
        android:padding='@dimen/padding_medium'
        android:text='@string/enable_broadcast_receiver'
        android:onClick='enableBroadcastReceiver'
        tools:context='.EnableDisableBroadcastReceiver' />

   <Button
       android:layout_width='fill_parent'
       android:layout_height='wrap_content'
       android:padding='@dimen/padding_medium'
       android:text='@string/disable_broadcast_receiver'
       android:onClick='disableBroadcastReceiver'
       tools:context='.EnableDisableBroadcastReceiver' />

</LinearLayout>

Resources

<resources>
    <string name='app_name'>EnableDisableBroadcastReceiver</string>
    <string name='enable_broadcast_receiver'>Enable Broadcast Receiver</string>
    <string name='disable_broadcast_receiver'>Disable Broadcast Receiver</string>
    <string name='start_repeating_alarm'>Start Repeating Alarm</string>
    <string name='cancel_alarm'>Cancel Alarm</string>
    <string name='menu_settings'>Settings</string>
    <string name='title_activity_enable_disable_boradcast_receiver'>EnableDisableBoradcastReceiver</string>
</resources>

Broadcast Receiver

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

    final public static String ONE_TIME = 'onetime';

    @Override
    public void onReceive(Context context, Intent intent) {

         //You can do the processing here update the widget/remote views.
         StringBuilder msgStr = new StringBuilder();
         //Format time.
         Format formatter = new SimpleDateFormat('hh:mm:ss a');
         msgStr.append(formatter.format(new Date()));

         Toast.makeText(context, msgStr, Toast.LENGTH_SHORT).show();

    }
}

Activity

public class EnableDisableBroadcastReceiver extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startRepeatingAlarm(View view)
    {
        AlarmManager am=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlarmManagerBroadcastReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
        //After after 2 seconds
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 4 , pi); 
        Toast.makeText(this, 'Started Repeating Alarm', Toast.LENGTH_SHORT).show();
    }

    public void cancelAlarm(View view)
    {
        Intent intent = new Intent(this, AlarmManagerBroadcastReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
        Toast.makeText(this, 'Cancelled alarm', Toast.LENGTH_SHORT).show();
    }

    public void enableBroadcastReceiver(View view){
        ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
        PackageManager pm = this.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
        Toast.makeText(this, 'Enabled broadcast receiver', Toast.LENGTH_SHORT).show();
    }

    public void disableBroadcastReceiver(View view){
        ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
        PackageManager pm = this.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        Toast.makeText(this, 'Disabled broadcst receiver', Toast.LENGTH_SHORT).show();
    }   
}
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
HelloCW
  • 843
  • 22
  • 125
  • 310

1 Answers1

1

Read Broadcastreceiver and Paused activity. It mentions that when you register a broadcast receiver programatically in an activity, it will NOT get broadcasts when the activity is paused.

You'll probably would want to use a Service if you want to run it in Background.

A Service is an application component that can perform long-running operations in the background and does not provide a user interface.

Have a service running and register the BroadcastReceiver there instead. You can register it in manifest. If you want to run it on reboot as well, then in you would need to use:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

and have it in intent-filter of your receiver:

<receiver android:enabled="true"           
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" android:name=".MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

If possible you can register the broadcast receiver in the manifest instead and handle it there. The receiver runs on the main thread, so ideally you can signal to a running service or start a service there. You might want to read Should an alarm be built with Service or BroadcastReceiver? .

Hope it helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Thanks! but in the document http://stackoverflow.com/questions/18995253/how-to-forward-sms-forever , I was told that the app will keep run after I install the app or reboot mobile. which one is right. –  Nov 07 '13 at 12:56
  • It depends what`actions` you have specified for your `BroadcasrReceiver`. Can you include you manifest in question? – Shobhit Puri Nov 07 '13 at 16:21
  • Thanks! If I include BroadcasrReceiver in my manifest, will the Broadcast receiver keep run after I have installed the .apk file even if I reboot mobile? – HelloCW Nov 08 '13 at 07:53
  • Yes, it should AFAIK and for making it work after rebooting, include `android.intent.action.BOOT_COMPLETED"` action. – Shobhit Puri Nov 08 '13 at 16:23