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