我正在尝试创建一个其他类可以重用并放入额外信息的通知 Activity 。我苦苦挣扎的是如何正确调用另一个类的方法。
通知 Activity 代码:
公共(public)类 NotificationTest 扩展了 MyActivity{
public void showNotification(String s, Context c) {
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
//Intent intent = new Intent(Sender context, which class to start);
Intent intent = new Intent(c, Settings.class);
//PendingIntent pIntent = PendingIntent.getActivity(sender context, 0, intent, 0);
PendingIntent pIntent = PendingIntent.getActivity(c, 0, intent, 0);
// this is it, we'll build the notification!
// in the addAction method, if you don't want any icon, just set the first param to 0
Notification mNotification = new Notification.Builder(c)
.setContentTitle("New Post!")
.setContentText(s)
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setSound(soundUri)
//.addAction(R.drawable.icon, "View",pIntent)
//.addAction(0, "Remind", pIntent)
.build();
Log.d("Vindsiden", "Notification");
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// If you want to hide the notification after it was selected, do the code below
// myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, mNotification);
}
}
从例如 MyActivity 调用通知的代码:
String s= "Notification";
NotificationTest notificationTest = new NotificationTest();
notificationTest.showNotification(s,MyActivity.this);
我得到的错误是:
12-05 19:20:34.004 22352-22352/com.vindsiden.windwidget E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.vindsiden.windwidget, PID: 22352
java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4531)
at com.vindsiden.windwidget.NotificationTest.showNotification(NotificationTest.java:66)
at com.vindsiden.windwidget.MyActivity$1.onClick(MyActivity.java:52)
at android.view.View.performClick(View.java:4424)
at android.view.View$PerformClick.run(View.java:18383)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
最佳答案
希望这对您有所帮助...我正在使用它。创建一个名为 OurNotification 的类
将所有 Menu.class 更改为您希望它在单击时继续的位置。
public class OurNotification {
public void createNotification(Context context, String title, String msg, String other) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification)
.setContentTitle(title)
.setContentText(msg);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, Menu.class);
// add so it removes
mBuilder.setAutoCancel(true);
//add vibrate and sound only use if you have the correct permissions in manifest or it will crash
// long[] vibrate = {0,100,200,300};
// mBuilder.setVibrate(vibrate);
mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
mBuilder.setDefaults(Notification.DEFAULT_LIGHTS);
mBuilder.setOnlyAlertOnce(true);
if (other != null) {
NotificationCompat.BigTextStyle bigStyle =
new NotificationCompat.BigTextStyle();
// Sets a title for the Inbox style big view
bigStyle.setBigContentTitle(other);
// Moves the big view style object into the notification object.
mBuilder.setStyle(bigStyle);
}
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(Menu.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(200, mBuilder.build());
}
}
然后在 Activity 服务或者哪里调用
new OurNotification notificaionToShow = new OurNotification();
notificaionToShow.createNotification(getApplicationContext(), "TITLE", "Message", null);
如果你想使用最后一个 null 的大样式更改为更多文本。
关于java - 发送通知到通知栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357879/