Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

android - BroadcastReceiver not working when app is not running

In my manifest file I have declared the receiver. (as follows)

<receiver android:name=".OnAlarmReceive" />

however, once I shut down my application, I am not able to get the alarms and the notifications. Apparently, a call to the OnReceive in my Broadcast receiver is never made.

public class OnAlarmReceive extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent arg1)
   {
       //various stuff
   }
}

Inside the MainActivity, my alarm manager class is as the follows.

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent("MY_ALARM_NOTIFICATION");
    intent.setClass(this, OnAlarmReceive.class);
    intent.putExtra("message", message);
    PendingIntent pendingIntent = PendingIntent
            .getBroadcast(MainActivity.this, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar timeCal = Calendar.getInstance();
    timeCal.set(Calendar.HOUR_OF_DAY, hour);
    timeCal.set(Calendar.MINUTE, minutes);

    alarmManager.set(AlarmManager.RTC_WAKEUP, timeCal.getTimeInMillis(), pendingIntent);

and my manifest as is follows :

    <receiver android:name=".OnAlarmReceive">
    <intent-filter android:priority="1">  
        <action android:name="MY_ALARM_NOTIFICATION"/>  
    </intent-filter>  
</receiver>  

What should I do in order to receive the notifications/alarms even if I have shut off my app. Background service ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you should add intent-filter in manifest,as

receiver android:name=".SmsBroadCastReceiver">  
        <intent-filter android:priority="20">  
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>  
        </intent-filter>  
    </receiver>  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...