单击Android通知后不打开新活动

时间:2013-10-01 05:23:04

标签: android notifications android-notifications

这里我想打开一个名为View的新活动类。一旦点击生成的通知后。我怎样才能做到这一点??一些立即回复将不胜感激。它之前工作得很好(在使用日历添加switch语句之前)现在我收到了通知。但是在点击通知

后没有打开新活动
    public class AlarmReceiver extends BroadcastReceiver {

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

        Log.v("Message", "Alarm");

        String lang = "";
        int imageCode = 1;

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

        Bitmap bitmap =  BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);  
        long when = System.currentTimeMillis();
        Notification notification;

        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

        Intent notificationIntent = new Intent(context, View.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        SharedPreferences  getPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean enableNotificaiton = getPrefs.getBoolean("notifications", true);        
        String timePeriod = getPrefs.getString("period", "2");
        Calendar calA = Calendar.getInstance();

        switch(Integer.parseInt(timePeriod)){

        case 1:
              //<item>Every 30 Minutes</item>                 
              calA.add(Calendar.MINUTE, 1);
              alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);               

            break;

        case 2:
             //<item>Hourly</item>
             calA.add(Calendar.MINUTE, 60);
             alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);  


            break;

        case 3:
             //<item>Every 2 Hours</item>
             calA.add(Calendar.MINUTE, 120);
             alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);


            break;

        case 4:
             //<item>Every 6 Hours</item>
             calA.add(Calendar.MINUTE, 60*6);
             alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);


            break;

        case 5:
             //<item>Daily</item>
             calA.add(Calendar.MINUTE, 60*24);
             alarmManager.set(AlarmManager.RTC_WAKEUP, calA.getTimeInMillis(), contentIntent);

            break;

        }

        String language = getPrefs.getString("language","1");

        switch (Integer.parseInt(language)) {

        case 1:
            lang = "en";
            break;

        case 2:
            lang = "sin";
            break;

        }   

        RemoteViews contentView = null;

        if (lang.equals("en")) {
            notification = new Notification(R.drawable.ic_launcher, "ProWeather - English", when);
            contentView = new RemoteViews(context.getPackageName(), R.layout.view);
            //contentView.setTextViewText(R.id.title, "English Notification");
        } else {

            notification = new Notification(R.drawable.ic_launcher, "ProWeather - Sinhala", when);
            contentView = new RemoteViews(context.getPackageName(), R.layout.view);     

            switch (imageCode) {
            case 1:
                Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sinhala);
                contentView.setImageViewBitmap(R.id.ivText, bitmap2);
                contentView.setTextViewText(R.id.tvDescrition, "Clear Weather");
                break;

            case 2:
                Bitmap bitmap3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sinhala2);
                contentView.setImageViewBitmap(R.id.ivText, bitmap3);
                contentView.setTextViewText(R.id.tvDescrition, "Heavy Rain");
                break;

            case 3:
                Bitmap bitmap4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.sinhala3);
                contentView.setImageViewBitmap(R.id.ivText, bitmap4);
                contentView.setTextViewText(R.id.tvDescrition, "Heavy Rain with Lightning");
                break;

            }
        }



        notification.contentView = contentView;

        notification.flags = Notification.FLAG_AUTO_CANCEL;

        notification.contentIntent = contentIntent;

        // Cancel the notification after its selected
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
        notification.defaults |= Notification.DEFAULT_VIBRATE; // Vibration
        notification.defaults |= Notification.DEFAULT_SOUND; // Sound

        // setting id a constant will replace previous notification with the
        // new
        notificationManager.notify(100, notification);


    }

}

3 个答案:

答案 0 :(得分:1)

使用以下代码:

        int icon = R.drawable.app_notification_icon;
        long when = System.currentTimeMillis();

        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);

        Intent notificationIntent = new Intent(context,
                NotificationDialog.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notificationIntent.putExtra(IntentConstantsUtils.MESSAGE, message);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);

创建一个新的java类:

    public class NotificationDialog extends Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            String message = getIntent().getStringExtra(IntentConstantsUtils.MESSAGE);
            showDialog(this, message, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
        }

private void showDialog(Context context, String message,
            DialogInterface.OnClickListener onOkClick) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle("Alert");

        // Setting Dialog Message
        alertDialog.setMessage(message);

        // Setting Icon to Dialog
        // alertDialog.setIcon(R.drawable.tick);

        // Setting OK Button
        alertDialog.setButton("OK", onOkClick);

        // Showing Alert Message
        alertDialog.show();
    }
    }

在清单添加

<activity
            android:name="com.shufflecloud.smg.activities.NotificationDialog"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Dialog" >
        </activity>

答案 1 :(得分:0)

更改

    Intent notificationIntent = new Intent(context, View.class);

    Intent notificationIntent = new Intent(context, View.class, Intent.FLAG_ACTIVITY_NEW_TASK);

答案 2 :(得分:0)

这是Kitkat 4.4中的问题 尝试使用以下行来点击通知

打开您想要打开的活动
android:exported="true"

并向通知inttent添加标志

Notification.FLAG_AUTO_CANCEL;