我有下一个代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
为什么它不起作用?它没有任何表现。在Android 2.2.1上进行测试。
更新。活动代码:
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
import android.view.View;
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void start(View view) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
}
}
布局中的按钮:
<Button
android:id="@+id/startButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_above="@+id/aboutButton"
android:layout_alignLeft="@+id/aboutButton"
android:layout_marginBottom="20dp"
android:onClick="start"
android:text="Start" />
UPDATE2。错误日志:
02-04 11:40:40.752: D/dalvikvm(23054): GC_EXTERNAL_ALLOC freed 859 objects / 59168 bytes in 61ms
02-04 11:40:56.424: D/AndroidRuntime(23054): Shutting down VM
02-04 11:40:56.424: W/dalvikvm(23054): threadid=1: thread exiting with uncaught exception (group=0x4001d888)
02-04 11:40:56.432: E/AndroidRuntime(23054): FATAL EXCEPTION: main
02-04 11:40:56.432: E/AndroidRuntime(23054): java.lang.IllegalStateException: Could not execute method of the activity
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View$1.onClick(View.java:2082)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View.performClick(View.java:2461)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View$PerformClick.run(View.java:8890)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.os.Handler.handleCallback(Handler.java:587)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.os.Looper.loop(Looper.java:123)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.app.ActivityThread.main(ActivityThread.java:4632)
02-04 11:40:56.432: E/AndroidRuntime(23054): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 11:40:56.432: E/AndroidRuntime(23054): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-04 11:40:56.432: E/AndroidRuntime(23054): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-04 11:40:56.432: E/AndroidRuntime(23054): at dalvik.system.NativeStart.main(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054): Caused by: java.lang.reflect.InvocationTargetException
02-04 11:40:56.432: E/AndroidRuntime(23054): at pckg.mywebsites.LoginActivity.start(LoginActivity.java:28)
02-04 11:40:56.432: E/AndroidRuntime(23054): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:40:56.432: E/AndroidRuntime(23054): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.view.View$1.onClick(View.java:2077)
02-04 11:40:56.432: E/AndroidRuntime(23054): ... 11 more
02-04 11:40:56.432: E/AndroidRuntime(23054): Caused by: java.lang.IllegalArgumentException: contentIntent required: pkg=pckg.mywebsites id=222 notification=Notification(vibrate=null,sound=null,defaults=0x0)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.os.Parcel.readException(Parcel.java:1264)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.os.Parcel.readException(Parcel.java:1248)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.app.NotificationManager.notify(NotificationManager.java:110)
02-04 11:40:56.432: E/AndroidRuntime(23054): at android.app.NotificationManager.notify(NotificationManager.java:90)
02-04 11:40:56.432: E/AndroidRuntime(23054): ... 15 more
答案 0 :(得分:12)
有效:
Intent resultIntent = new Intent(this, LoginActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
getApplicationContext()).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(resultPendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(FM_NOTIFICATION_ID, mBuilder.build());
答案 1 :(得分:0)
在为API 11之前的版本编写代码时,您应该包含支持库,而不是调用Notification.Builder
,而是需要调用NotificationCompact
。
尝试阅读Android support Library page并阅读示例项目以获取这些内容。
<强>编辑:强>
尝试以下代码:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Notifications Example") .setContentText("This is a test notification"); // Add as notification NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(FM_NOTIFICATION_ID, builder.build());