我知道有很多话题。我知道有Android开发者网站,即使我使用这个网站的代码我也有错误。
我只想在通知中加入ProgressBar。 我把这个方法叫做Android Developer网站:
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 com.android.internal.policy.impl.PhoneWindow$DecorView@410c2238 that was originally added here
E/WindowManager(9840): android.view.WindowLeaked: Activity com.main.LoginConnection has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@410c2238 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 com.android.internal.policy.impl.PhoneWindow$DecorView@41162188 that was originally added here
E/WindowManager(9840): android.view.WindowLeaked: Activity com.main.LoginConnection has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41162188 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 :(得分:0)
首先,我建议使用NotificationCompat.Builder
,而不仅仅是Notification.Builder
- 这可能是您遇到的第一个问题(您是否忽略了警告?)。您需要app compat v7并支持v4库。
其次,您不会看到通知。通知需要以下内容才能实际发布:
要查看您的通知,请添加内容Intent setContentIntent(PendingIntent.getActivity(context, 0, new Intent(), PendingIntent.FLAG_ONE_SHOT))
和小图标setSmallIcon(android.R.drawable.ic_notification_clear_all
- 然后您就可以发布它了。
通过进行这些修改,您的代码适合我。您还有其他一些问题,特别是在您完成后不会删除您的下载通知,但这就像将notify(100
来电更改为notify(0
一样简单,您就是#&# 39;重新传递的是通知ID,因此如果它应该替换进度,则需要匹配。
我没有修复你的内存泄漏,这是一个不挂在你的线程中的活动上下文的问题。