今天写notification练习时,误将NotificationManager.notify(0, notification);写成notification.notify(); 代码如下
public void notification() {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Builder(this);
builder.setAutoCancel(true);
builder.setContentTitle("通知")
.setContentText("拨打电话")
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(
BitmapFactory.decodeResource(getResources(),
R.drawable.call));
Notification notification = builder.getNotification();
//nm.notify(0, notification);
notification.notify();
}
错误日志
02-14 08:14:55.771: E/AndroidRuntime(25572): FATAL EXCEPTION: main
02-14 08:14:55.771: E/AndroidRuntime(25572): java.lang.IllegalStateException: Could not execute method of the activity
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.view.View$1.onClick(View.java:3598)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.view.View.performClick(View.java:4091)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.view.View$PerformClick.run(View.java:17072)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.os.Handler.handleCallback(Handler.java:615)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.os.Looper.loop(Looper.java:153)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.app.ActivityThread.main(ActivityThread.java:5000)
02-14 08:14:55.771: E/AndroidRuntime(25572): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 08:14:55.771: E/AndroidRuntime(25572): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 08:14:55.771: E/AndroidRuntime(25572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
02-14 08:14:55.771: E/AndroidRuntime(25572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
02-14 08:14:55.771: E/AndroidRuntime(25572): at dalvik.system.NativeStart.main(Native Method)
02-14 08:14:55.771: E/AndroidRuntime(25572): Caused by: java.lang.reflect.InvocationTargetException
02-14 08:14:55.771: E/AndroidRuntime(25572): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 08:14:55.771: E/AndroidRuntime(25572): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.view.View$1.onClick(View.java:3593)
02-14 08:14:55.771: E/AndroidRuntime(25572): ... 11 more
02-14 08:14:55.771: E/AndroidRuntime(25572): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before notify()
02-14 08:14:55.771: E/AndroidRuntime(25572): at java.lang.Object.notify(Native Method)
02-14 08:14:55.771: E/AndroidRuntime(25572): at com.example.notificationtest.MainActivity.notification(MainActivity.java:37)
02-14 08:14:55.771: E/AndroidRuntime(25572): ... 14 more
错误很明显:object not locked by thread before notify() , 对象在notify(唤醒)前没有被锁死,查看notify源码,进入Object类中(居然在Object中,一定是调用错了)
/**
* Causes a thread which is waiting on this object‘s monitor (by means of
* calling one of the {@code wait()} methods) to be woken up. If more than
* one thread is waiting, one of them is chosen at the discretion of the
* VM. The chosen thread will not run immediately. The thread
* that called {@code notify()} has to release the object‘s monitor first.
* Also, the chosen thread still has to compete against other threads that
* try to synchronize on the same object.
...
*/
public final native void notify();
仔细一看才发现,Object.notify()和wait是用于对对象同步操作用的方法,和notification完全不搭嘎。
我们再来看看NotificationManager.notify(0, notification)方法,进入NotificationManager中,
/**
* Post a notification to be shown in the status bar. If a notification with
* the same id has already been posted by your application and has not yet been canceled, it
* will be replaced by the updated information.
...
*/
public void notify(int id, Notification notification),从注释我们可以发现此方法用于显示通知,并且如果id相同的话,用新的notification替换原先的notification