我想当用户点击通知它返回应用程序或开始新活动时,我在这里尝试了所有代码,但它突然关闭了我的应用程序。如果有人可以帮我修改我的代码,我将不胜感激。感谢你 。
Calendar calendar = Calendar.getInstance();
Intent intent;
PendingIntent pendingIntent;
AlarmManager alarmManager;
long futureInMillis;
// SharedPreferences sharedPreferences = null;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
int uniqueCode = preferences.getInt("uniqueCode", 0) + 1;
preferences.edit().putInt("uniqueCode", uniqueCode).apply();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month); // January has value 0
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
//calendar.set(Calendar.AM_PM, Calendar.AM );
//futureInMillis = calendar.getTimeInMillis();
//futureInMillis = SystemClock.elapsedRealtime() + (value * 1000);
intent = new Intent(getActivity(), NotificationReceiver.class);
intent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1);
intent.putExtra(NotificationReceiver.NOTIFICATION, getNotification(" "+taskn+" moved to q1 "));
pendingIntent = PendingIntent.getBroadcast(getActivity(), uniqueCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
return rootView;
}
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(getActivity());
query.whereEqualTo("TaskN",taskn);
query.whereEqualTo("TheUser", username);
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, com.parse.ParseException e) {
object.put("Quadric", "q1");
object.saveInBackground();
}
});
builder.setContentTitle("Time management.");
builder.setContentText(content);
// notification.setLatestEventInfo(context, title, text, contentIntent);
// builder.setSound();
builder.setSmallIcon(R.drawable.ic_home);
return builder.build();
}
我在logCat中得到这个:
04-02 13:42:24.161 4030-4030/info.androidhive.slidingmenu E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
04-02 13:42:24.161 4030-4030/info.androidhive.slidingmenu E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
04-02 13:42:24.173 4030-4030/info.androidhive.slidingmenu E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
04-02 13:42:24.185 4030-4030/info.androidhive.slidingmenu E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
04-02 13:49:50.309 4118-4118/info.androidhive.slidingmenu E/TaskStackBuilder﹕ Bad ComponentName while traversing activity parent metadata
04-02 13:49:50.309 4118-4118/info.androidhive.slidingmenu E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: info.androidhive.slidingmenu, PID: 4118
java.lang.IllegalArgumentException: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.NotificationActivity}
at android.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:180)
at android.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:151)
at info.androidhive.slidingmenu.NotificationFragment.onCreateView(NotificationFragment.java:80)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.pm.PackageManager$NameNotFoundException: ComponentInfo{info.androidhive.slidingmenu/ info.androidhive.slidingmenu.NotificationActivity}
at android.app.ApplicationPackageManager.getActivityInfo(ApplicationPackageManager.java:242)
at android.app.TaskStackBuilder.addParentStack(TaskStackBuilder.java:167)
当我尝试这段代码时:
intent = new Intent(getActivity(), NotificationReceiver.class);
intent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1);
intent.putExtra(NotificationReceiver.NOTIFICATION, getNotification(" "+taskn+" moved to q1 "));
pendingIntent = PendingIntent.getBroadcast(getActivity(), uniqueCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getActivity());
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(NotificationActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(intent);
pendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(getActivity());
query.whereEqualTo("TaskN",taskn);
query.whereEqualTo("TheUser", username);
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, com.parse.ParseException e) {
object.put("Quadric", "q1");
object.saveInBackground();
}
});
builder.setContentTitle("Time management.");
builder.setContentText(content);
// notification.setLatestEventInfo(context, title, text, contentIntent);
// builder.setSound();
builder.setSmallIcon(R.drawable.ic_home);
return builder.build();
}
答案 0 :(得分:0)
尝试使用我的代码创建通知。
Intent notificationIntent = new Intent(getBaseContext(), MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(), 0,
notificationIntent, 0);
Intent resultIntent = new Intent(getBaseContext(), MainActivity2.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(getBaseContext(), 0,
resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);
// Set the Notification's Click Behavior
//mBuilder.setContentIntent(resultPendingIntent);
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.xxxx);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = Constants.NOTIFICATION_ID.FOREGROUND_SERVICE;
NotificationCompat.Builder mNotifyBuilder;
if (Build.VERSION.SDK_INT >= 21) {
mNotifyBuilder = new NotificationCompat.Builder(getBaseContext())
.setContentTitle("My App name")
.setTicker("My App descrition")
.setSmallIcon(R.drawable.xxxx)
.setLargeIcon(
Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.setVisibility(Notification.VISIBILITY_SECRET) // doesn't show any part of this notification on the lock screen.
.addAction(android.R.drawable.ic_menu_view, getBaseContext().getResources().getString(R.string.foreground_notification_dim), resultPendingIntent);
mNotificationManager.notify(
notifyID,
mNotifyBuilder.build());
}