2013-02-04 90 views
4

我有下面的代碼:NotificationCompat.Builder不起作用,安卓2.2.1

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 
+1

例外的說,它需要一個內容意圖和你不提供一個 – Zharf

+0

可能的重複[Is setContentIntent(P.在NotificationCompat.Builder?中是必需的](http://stackoverflow.com/questions/20032249/is-setcontentintentpendingintent-required-in-notificationcompat-builder) – GrIsHu

回答

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()); 
+0

謝謝你的回答@Maxim Pochtar –

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()); 
+0

它編譯好了,我使用android.support.v4.app。 NotificationCompat。當我在手機上啓動應用程序時,我稱之爲「啓動」功能時什麼都不顯示。 –

+0

你能發表更多的代碼嗎? – GrIsHu

+0

更新第一篇文章。 –