单击通知Android后未打开新活动 [英] Not Opening the New Activity After Clicking on the Notification Android

查看:81
本文介绍了单击通知Android后未打开新活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here i want to open a new activity class called View. Once after click on the generated notification. How can i do that?? Some immediate reply will be appreciated. It worked well earlier(before add switch statements with calendar) Now i`m getting the notification. but not opening the new activity after clicking on the notification

    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);


    }

}

推荐答案

使用以下代码:

        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类:

Create a new java class:

    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>

这篇关于单击通知Android后未打开新活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆