0

在这里,我想打开一个名为 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);


    }

}
4

3 回答 3

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>
于 2013-10-01T05:31:30.217 回答
0

这是 Kitkat 4.4 中的问题,请尝试在单击通知时打开您想要打开的活动的以下行

android:exported="true"

并将标志添加到通知意图

Notification.FLAG_AUTO_CANCEL;
于 2015-04-17T07:07:24.483 回答
0

改变

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

    Intent notificationIntent = new Intent(context, View.class, Intent.FLAG_ACTIVITY_NEW_TASK);
于 2014-12-05T20:20:17.013 回答