如何在android通知警报

时间:2015-05-27 11:50:10

标签: android android-intent android-notifications

我的查询是,我想这取决于不同的条件来重定向通知意图,我报警系统,我需要重定向通知意图一些活动上工作说A.如果用户在报警过程中点击它播放和完成后,如果仍然可以通知,则应将通知重定向到其他一些活动。这是我的代码。任何帮助将不胜感激!

   private void DisplayNotification(String AlarmName, Context context) {
    // Context context = context.getApplicationContext();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Intent mIntent;
    SharedPreferences alarmindicator = context.getSharedPreferences(
            "notifyintent", context.MODE_PRIVATE);
    int code;
    SharedPreferences.Editor editor = alarmindicator.edit();
    if (alarmindicator.getBoolean("notifyintentPlaying", true)) {
        mIntent = new Intent(context, AlarmAlertActivity.class);
        Toast.makeText(context, "inalmact", 0).show();
        code = 1;
    }

    else {
        editor.putBoolean("notifyintentPlaying", true);
        editor.commit();
        mIntent = new Intent(context, MainActivity.class);
        // AlarmAlertActivity.this.finish();
        code = 2;
        Toast.makeText(context, "in main act", 0).show();

    }

    Bundle bundle = new Bundle();

    bundle.putString("test", "test");
    mIntent.putExtras(bundle);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, code,
            mIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Resources res = context.getResources();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            context);

    builder.setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.notifyiconlarg)
            .setLargeIcon(
                    BitmapFactory.decodeResource(res,
                            R.drawable.notifyiconlarg))
            // .setTicker(res.getString(R.string.notification_title))
            .setTicker(AlarmName).setAutoCancel(true)
            .setContentTitle(AlarmName)
            .setContentText("Time to offer " + AlarmName + " Prayers");

    notificationManager = (NotificationManager) context
            .getSystemService(context.NOTIFICATION_SERVICE);

    notificationManager.notify(123, builder.build());
}

1 个答案:

答案 0 :(得分:2)

您必须从通知中触发广播接收器,并且onReceive()方法BroadcastReceiver您必须决定启动哪个活动....

<强>更新

像这样写一个BroadcastReceiver

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int code;
        Intent mIntent;
        SharedPreferences.Editor editor = alarmindicator.edit();
        if (alarmindicator.getBoolean("notifyintentPlaying", true)) {
            mIntent = new Intent(context, AlarmAlertActivity.class);
            Toast.makeText(context, "inalmact", 0).show();
            code = 1;
        } else {
            editor.putBoolean("notifyintentPlaying", true);
            editor.commit();
            mIntent = new Intent(context, MainActivity.class);
            // AlarmAlertActivity.this.finish();
            code = 2;
            Toast.makeText(context, "in main act", 0).show();
        }
        context.startActivity(intent);
    }

并像这样创建Pending意图

Intent myIntent=new Intent("some_action")
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, code,
                myIntent, PendingIntent.FLAG_UPDATE_CURRENT);