Android Broadcast Reciever打开一个意图

时间:2015-11-06 03:29:59

标签: android android-intent broadcastreceiver

我有一个Android应用程序需要定期生成通知,当我们点击通知时,通知中显示的消息将显示在Original应用程序活动内的TextView内。

为此,我创建了一个 AlarmReceiver.java类

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Log.d("ME", "Notification started");

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);


            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                                                    .setSmallIcon(R.drawable.ic_noticon)
                                                    .setContentTitle("Notification")
                                                    .setContentText("You have to open app to see this message in detail");


            NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mNotificationManager.notify(1, mBuilder.build());


            Intent resultIntent = new Intent("com.app.AlarmBack");
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            stackBuilder.addParentStack(AlarmBack.class);
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_ONE_SHOT);
            mBuilder.setContentIntent(resultPendingIntent);


    }    

我也为名为 AlarmBack.java

的意图创建了一个类
public class AlarmBack extends MainActivity {


TextView tv_alert_back_display;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);


    getLayoutInflater().inflate(R.layout.alert_back, frameLayout);
    mDrawerList.setItemChecked(position, true);
    setTitle(mTitle);


    tv_alert_back_display = (TextView) findViewById(R.id.tv_alert_back_display);


    tv_alert_back_display.setText("This is the message that needs your attension");
}}

但问题是这不起作用。它没有打开想要打开它的意图。那么,有人可以帮助解决我的问题。

3 个答案:

答案 0 :(得分:1)

您需要在构建器上设置意图后调用notify(),而不是之前。否则,builder.build()创建的通知在添加到通知管理器时将不会有这些意图。

答案 1 :(得分:1)

在AlarmReceiver OnRecieve方法中调用此方法

int taskID = 1;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(taskID, getNotification(context, "title", "content"));

并在警报接收器中添加此方法

private Notification getNotification(Context context, String title, String content) {
    Notification.Builder builder = new Notification.Builder(context);
    builder.setContentTitle(title);
    builder.setContentText(content);
    builder.setSmallIcon(R.drawable.icon_64);
    builder.setAutoCancel(true);
    builder.setDefaults(android.app.Notification.DEFAULT_ALL);


    Intent resultIntent = new Intent(context, SplashActivity.class); //Activity that you want to open
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(SplashActivity.class);              // Activity that you want to open
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(
                    0,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    builder.setContentIntent(resultPendingIntent);
    return builder.getNotification();
}

答案 2 :(得分:0)

setContentIntent

需要NotificationBuilder
NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context).setSmallIcon(R.drawable.app_icon)
            .setContentTitle("Your Title").setContentText("");

    Intent intent = new Intent(context, YourMainActivity.class);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);