2014-10-01 91 views
0

我知道有很多關於它的話題。我知道有Android開發者網站,即使我使用這個網站的代碼,我有一個錯誤。通知中的ProgressBar [ANDROID]

我只是想在通知中放置一個ProgressBar。 所以我把這種方法,我從Android開發者網站了:

public static void generateNotification(Context context){ 

    final NotificationManager mNotifyManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    final Notification.Builder mBuilder = new Notification.Builder(context); 
    mBuilder.setContentTitle("Picture Download") 
      .setContentText("Download in progress"); 
    // Start a lengthy operation in a background thread 
    new Thread(
     new Runnable() { 
      @Override 
      public void run() { 
       int incr; 

       for (incr = 0; incr <= 100; incr+=5) { 

         mBuilder.setProgress(100, incr, false);       
         mNotifyManager.notify(0, mBuilder.build()); 

          try { 

           Thread.sleep(5*1000); 
          } catch (InterruptedException e) { 
           Log.d("Generate Notif", "sleep failure"); 
          } 
       } 
       // When the loop is finished, updates the notification 
       mBuilder.setContentText("Download complete") 
       .setProgress(0,0,false); 
       mNotifyManager.notify(100, mBuilder.build()); 
      } 
     } 
    // Starts the thread by calling the run() method in its Runnable 
    ).start(); 
} 

,我有此錯誤:

10-01 15:37:42.226: E/AndroidRuntime(9840): FATAL EXCEPTION: Thread-1250 
10-01 15:37:42.226: E/AndroidRuntime(9840): java.lang.NoSuchMethodError: android.app.Notification$Builder.build 
10-01 15:37:42.226: E/AndroidRuntime(9840): at com.Tools.UtilClass$10.run(UtilClass.java:813) 
10-01 15:37:42.226: E/AndroidRuntime(9840): at java.lang.Thread.run(Thread.java:856) 

這:

E/WindowManager(9840): Activity com.main.LoginConnection has leaked window [email protected] that was originally added here 
E/WindowManager(9840): android.view.WindowLeaked: Activity com.main.LoginConnection has leaked window [email protected] that was originally added here 
E/WindowManager(9840): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344) 
E/WindowManager(9840): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267) 
E/WindowManager(9840): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 
E/WindowManager(9840): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 
E/WindowManager(9840): at android.view.Window$LocalWindowManager.addView(Window.java:537) 
E/WindowManager(9840): at android.app.Dialog.show(Dialog.java:278) 
E/WindowManager(9840): at com.main.LoginConnection.getRegisterId(LoginConnection.java:281) 
E/WindowManager(9840): at com.main.LoginConnection.onCreate(LoginConnection.java:101) 
E/WindowManager(9840): at android.app.Activity.performCreate(Activity.java:4465) 
E/WindowManager(9840): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
E/WindowManager(9840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
E/WindowManager(9840): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
E/WindowManager(9840): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
E/WindowManager(9840): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
E/WindowManager(9840): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/WindowManager(9840): at android.os.Looper.loop(Looper.java:137) 
E/WindowManager(9840): at android.app.ActivityThread.main(ActivityThread.java:4424) 
E/WindowManager(9840): at java.lang.reflect.Method.invokeNative(Native Method) 
E/WindowManager(9840): at java.lang.reflect.Method.invoke(Method.java:511) 
E/WindowManager(9840): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
E/WindowManager(9840): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
E/WindowManager(9840): at dalvik.system.NativeStart.main(Native Method) 
E/WindowManager(9840): Activity com.main.LoginConnection has leaked window [email protected] that was originally added here 
E/WindowManager(9840): android.view.WindowLeaked: Activity com.main.LoginConnection has leaked window [email protected] that was originally added here 
E/WindowManager(9840): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344) 
E/WindowManager(9840): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267) 
E/WindowManager(9840): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215) 
E/WindowManager(9840): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140) 
E/WindowManager(9840): at android.view.Window$LocalWindowManager.addView(Window.java:537) 
E/WindowManager(9840): at android.app.Dialog.show(Dialog.java:278) 
E/WindowManager(9840): at com.Tools.UtilClass.openSettingApp(UtilClass.java:557) 
E/WindowManager(9840): at com.Tools.UtilClass$4.onClick(UtilClass.java:383) 
E/WindowManager(9840): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) 
E/WindowManager(9840): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/WindowManager(9840): at android.os.Looper.loop(Looper.java:137) 
E/WindowManager(9840): at android.app.ActivityThread.main(ActivityThread.java:4424) 
E/WindowManager(9840): at java.lang.reflect.Method.invokeNative(Native Method) 
E/WindowManager(9840): at java.lang.reflect.Method.invoke(Method.java:511) 
E/WindowManager(9840): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
E/WindowManager(9840): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
E/WindowManager(9840): at dalvik.system.NativeStart.main(Native Method) 

,所以我不明白爲什麼我有這個錯誤...

回答

0

首先,我會建議使用NotificationCompat.Builder,而不僅僅是Notification.Builder--這可能是您遇到的第一個問題(您是否忽略了警告?)。您將需要應用程序compat v7和支持v4庫。

其次,你不會看到通知。通知需要以下實際崗位:

  1. 內容標題
  2. 內容文本
  3. 內容意圖
  4. 小圖標

要查看通知,添加內容意圖setContentIntent(PendingIntent.getActivity(context, 0, new Intent(), PendingIntent.FLAG_ONE_SHOT))和小圖標setSmallIcon(android.R.drawable.ic_notification_clear_all - 然後你就可以發佈它了。

通過進行這些修改,您的代碼適用於我。您還有其他一些問題,特別是當您完成後不會刪除您的下載通知,但這與將您的notify(100呼叫更改爲notify(0一樣簡單,您傳遞的號碼是通知ID,因此它們需要匹配if它應該取代進展之一。

我還沒有解決你的內存泄漏,這是一個不掛在你的線程內的活動上下文的問題。

+0

我寵愛NOtificationCompat.Builder,但如果我把這個代碼,我有setProgress方法的錯誤'這個方法是未定義的......'所以我怎麼做,而我把這段代碼放在ANdroid開發者網站上 – 2014-10-02 08:24:40

+0

我發現問題。這是因爲我有android-support-v13和 顯然我應該使用android-support-V4。 BUt此時沒有任何反應。 – 2014-10-02 09:06:17

+0

正如我所說的,您需要使用NotificationCompat.Builder,並且您需要提到的4件事情才能發佈通知。如果你沒有全部4個,它不會發布。 – Travis 2014-10-02 13:38:14