来自服务的通知

时间:2014-06-15 23:01:02

标签: android notifications android-service

我尝试通过其他操作发送通知。我在服务中注册它:

IntentFilter filter = new IntentFilter();
filter.addAction(ACT_ACCEPT);
receiver = new notifReciever();
this.registerReceiver(receiver, filter);

我的BroadcastReceiver:

public class notifReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("BR_Action", intent.getAction());
    }
};

我这样发送:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
    new Intent(this, MainActivity.class)
    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
    PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent contentIntentAccept = PendingIntent.getActivity(getApplicationContext(), 0,
    new Intent(this, AppService.class).setAction(ACT_ACCEPT), PendingIntent.FLAG_CANCEL_CURRENT);

n = new Notification.Builder(getApplicationContext())
    .setContentTitle("Test")
    .setContentText(msg)
    .setSmallIcon(R.drawable.ic_launcher)
    .setAutoCancel(true)
    .setVibrate(pattern)
    .addAction(R.drawable.ic_stat_accept, "Accept", contentIntentAccept)
    .setStyle(new Notification.BigTextStyle().bigText(msg))
    .setContentIntent(contentIntent).build();   

但标记为"BR_Action"的字符串未添加到日志中。

1 个答案:

答案 0 :(得分:1)

只有在启动Intent时才会调用

onReceive(),而不只是注册它。尝试使用startActivity(new Intent(ACT_ACCEPT));

相关问题