本文介绍了广播接收器不能显示通知,当应用程序关闭/丧生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是设置一个报警触发接收器显示通知的具体时间。

如果应用程序被打开或只是在背景中的code完美的作品。

当时应用程序是被杀害,接收器仍然可以执行报警时触发。

但是,没有通知被证明。

我该怎么办?

报警功能:

 公共静态无效makeAlarm(上下文的背景下,日期为准){
    Log.i(报警,makeAlarm);
    AlarmManager alarmMgr;
    的PendingIntent alarmIntent;
    alarmMgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    意向意图=新意图(背景下,AlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(上下文,0,意图,0);
    alarmMgr.set(AlarmManager.RTC_WAKEUP,
            date.getTime(),alarmIntent);
}

AlarmReceiver

 公共类AlarmReceiver扩展广播接收器{
    公共AlarmReceiver(){
    }    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
                Log.i(报警,的onReceive);
                android.support.v4.app.NotificationCompat.Builder mBuilder =
                        新NotificationCompat.Builder(上下文)
                                .setSmallIcon(R.drawable.app_logo)
                                .setContentTitle(标题)
                                .setContentText(信息);                意图resultIntent =新意图(背景下,MainActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(上下文);
                stackBuilder.addParentStack(MainActivity.class);
                stackBuilder.addNextIntent(resultIntent);
                的PendingIntent resultPendingIntent =
                        stackBuilder.getPendingIntent(
                                0,
                                PendingIntent.FLAG_UPDATE_CURRENT
                        );
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager =
                        (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
                随机兰特=新的随机();
                mNotificationManager.notify(rand.nextInt(),mBuilder.build());    }
}

的Manifest.xml

 <接收
    机器人:名字=。receiver.AlarmReceiver
    机器人:启用=真
    机器人:出口=真/>


解决方案

请运行示例code。我认为它可以作为你想要的。

 公共类MainActivity扩展AppCompatActivity {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    setUpAlarm(10 * 1000);
}
公共无效setUpAlarm(长triggerTimeInMS){
    AlarmManager alarmManager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    意向意图=新意图(这一点,AlarmReceiver.class);
    的PendingIntent的PendingIntent = PendingIntent.getBroadcast(这一点,1,意向,0);
    alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis的()+ triggerTimeInMS,的PendingIntent);
}
}

创建接收器类 -

 公共类AlarmReceiver扩展广播接收器{
私人字符串标记=AlarmLog;@覆盖
公共无效的onReceive(上下文的背景下,意图意图){    Toast.makeText(背景下,来电接收,Toast.LENGTH_LONG).show();    showNotification(上下文);}私人无效showNotification(上下文的背景下){
    乌里soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);    NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(上下文)
            .setSmallIcon(R.drawable.alarm)
            .setContentTitle(通知!)//称号的通知
            .setContentText(报警接收)
            .setSound(soundUri)//消息通知
            .setAutoCancel(真); //点​​击清除后通知
        NotificationManager mNotificationManager =
            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0,mBuilder.build());
}
}

不要忘了注册器接收器。里面的manifest.xml将低于code -

 <应用
    机器人:allowBackup =真
    机器人:图标=@的mipmap / ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:supportsRtl =真
    机器人:主题=@风格/ AppTheme>
    <活动机器人:名字=。MainActivity>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <接收机器人:名字=com.example.shajib.alarmtest.AlarmReceiver/>
< /用途>

My task is to set a alarm to trigger receiver to show notification a specific time.

And the code works perfectly if app is opened or just in background.

At the time app is being killed, receiver can still be triggered when alarm is executed.

However, no notification was shown.

What can I do?

Alarm function:

public static void makeAlarm(Context context,Date date) {
    Log.i("Alarm", "makeAlarm");
    AlarmManager alarmMgr;
    PendingIntent alarmIntent;
    alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    alarmMgr.set(AlarmManager.RTC_WAKEUP,
            date.getTime(), alarmIntent);
}

AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {
    public AlarmReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
                Log.i("Alarm", "onReceive");
                android.support.v4.app.NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(context)
                                .setSmallIcon(R.drawable.app_logo)
                                .setContentTitle("Title")
                                .setContentText("Message");

                Intent resultIntent = new Intent(context, MainActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
                stackBuilder.addParentStack(MainActivity.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent =
                        stackBuilder.getPendingIntent(
                                0,
                                PendingIntent.FLAG_UPDATE_CURRENT
                        );
                mBuilder.setContentIntent(resultPendingIntent);
                NotificationManager mNotificationManager =
                        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                Random rand = new Random();
                mNotificationManager.notify(rand.nextInt(), mBuilder.build());

    }
}

Manifest.xml

<receiver
    android:name=".receiver.AlarmReceiver"
    android:enabled="true"
    android:exported="true" />
解决方案

Please run the sample code. I think it works as you want.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setUpAlarm(10 * 1000);
}
public void setUpAlarm(long triggerTimeInMS) {
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1,  intent, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+triggerTimeInMS , pendingIntent);
}
}

Create the receiver class -

public class AlarmReceiver extends BroadcastReceiver {
private String TAG = "AlarmLog";

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

    Toast.makeText(context, "Incoming Call Received", Toast.LENGTH_LONG).show();

    showNotification(context);

}

private void showNotification(Context context) {
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.alarm)
            .setContentTitle("Notification!") // title for notification
            .setContentText("Alarm Received")
            .setSound(soundUri)// message for notification
            .setAutoCancel(true); // clear notification after click
        NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
}
}

don't forget tor register the receiver . inside manifest.xml put the below code-

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name="com.example.shajib.alarmtest.AlarmReceiver" />
</application>

这篇关于广播接收器不能显示通知,当应用程序关闭/丧生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 21:22