2011-04-27 92 views
2

我已經嘗試過,今天試過只是爲了得到一個帶有2個按鈕和一個TextView的自定義消息。 我得到了XML警告。但我想使用的代碼只給我強制關閉Android開發:如何製作自定義狀態欄通知?

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.main); 
     contentView.setImageViewResource(R.id.Button01, R.drawable.icon); 
     contentView.setTextViewText(R.id.Button01, "Hello, this message is in a custom expanded view"); 

Intent notificationIntent = new Intent(this, main.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

和IM漂亮丟失,所以請不要把我送到http://developer.android.com/guide/topics/ui/notifiers/notifications.html 我用盡。

如果有人能夠告訴我從開始,我真的很高興,因爲我一整天都在嘗試這種方式,並且真的會非常感激一個很好的答案!

謝謝!

//盧卡斯

編輯* FULL代碼,VIKTOR

import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.RemoteViews; 

public class main extends Activity { 
    /** Called when the activity is first created. */ 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.main); 

     int icon = R.drawable.icon; 
     CharSequence tickerText = "Hello"; 
     long when = System.currentTimeMillis(); 

     Notification notification = new Notification(icon, tickerText, when); 

     notification.flags |= Notification.FLAG_ONGOING_EVENT; 


     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.main); 
     contentView.setImageViewResource(R.id.Button01, R.drawable.icon); 
     contentView.setTextViewText(R.id.Button01, "Hello, this message is in a custom expanded view"); 


     notification.contentView = contentView; 

     Intent notificationIntent = new Intent(this, main.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

     notification.contentIntent = contentIntent; 
     final int CUSTOM_VIEW_ID = 1; 
     mNotificationManager.notify(CUSTOM_VIEW_ID, notification); 

    } 
} 

堆棧跟蹤:

04-27 14:43:07.035: ERROR/StatusBarService(337): couldn't inflate view for notification Com.haxxzor.multitask/0x1 
04-27 14:43:07.035: ERROR/StatusBarService(337): android.view.InflateException: Binary XML file line #1: Error inflating class android.widget.HorizontalScrollView 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at android.view.LayoutInflater.createView(LayoutInflater.java:518) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at android.view.LayoutInflater.inflate(LayoutInflater.java:386) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at android.widget.RemoteViews.apply(RemoteViews.java:939) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at com.android.systemui.statusbar.StatusBarService.makeNotificationView(StatusBarService.java:524) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at com.android.systemui.statusbar.StatusBarService.addNotificationViews(StatusBarService.java:553) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at com.android.systemui.statusbar.StatusBarService.addNotification(StatusBarService.java:382) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at com.android.systemui.statusbar.CommandQueue$H.handleMessage(CommandQueue.java:178) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at android.os.Looper.loop(Looper.java:123) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at dalvik.system.NativeStart.main(Native Method) 
04-27 14:43:07.035: ERROR/StatusBarService(337): Caused by: android.view.InflateException: Binary XML file line #1: Class not allowed to be inflated android.widget.HorizontalScrollView 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at android.view.LayoutInflater.failNotAllowed(LayoutInflater.java:530) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  at android.view.LayoutInflater.createView(LayoutInflater.java:477) 
04-27 14:43:07.035: ERROR/StatusBarService(337):  ... 17 more 
04-27 14:43:10.113: ERROR/AndroidRuntime(815): FATAL EXCEPTION: main 
04-27 14:43:10.113: ERROR/AndroidRuntime(815): android.app.RemoteServiceException: Bad notification posted from package Com.haxxzor.multitask: Couldn't expand RemoteViews for: StatusBarNotification(package=Com.haxxzor.multitask id=1 tag=null notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x2)) 
04-27 14:43:10.113: ERROR/AndroidRuntime(815):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1044) 
04-27 14:43:10.113: ERROR/AndroidRuntime(815):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-27 14:43:10.113: ERROR/AndroidRuntime(815):  at android.os.Looper.loop(Looper.java:123) 
04-27 14:43:10.113: ERROR/AndroidRuntime(815):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-27 14:43:10.113: ERROR/AndroidRuntime(815):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-27 14:43:10.113: ERROR/AndroidRuntime(815):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-27 14:43:10.113: ERROR/AndroidRuntime(815):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-27 14:43:10.113: ERROR/AndroidRuntime(815):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-27 14:43:10.113: ERROR/AndroidRuntime(815):  at dalvik.system.NativeStart.main(Native Method) 
04-27 14:47:12.065: ERROR/InputDispatcher(44): channel '40643090 com.notif/com.notif.main (server)' ~ Consumer closed input channel or an error occurred. events=0x8 
04-27 14:47:12.065: ERROR/InputDispatcher(44): channel '40643090 com.notif/com.notif.main (server)' ~ Channel is unrecoverably broken and will be disposed! 
04-27 14:47:31.551: ERROR/AndroidRuntime(885): FATAL EXCEPTION: main 
04-27 14:47:31.551: ERROR/AndroidRuntime(885): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.notif/com.notif.main}: java.lang.IllegalMonitorStateException: object not locked by thread before notify() 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at android.os.Handler.dispatchMessage(Handler.java:99) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at android.os.Looper.loop(Looper.java:123) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at dalvik.system.NativeStart.main(Native Method) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before notify() 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at java.lang.Object.notify(Native Method) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at com.notif.main.onCreate(main.java:27) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
04-27 14:47:31.551: ERROR/AndroidRuntime(885):  ... 11 more 
+0

你可以發佈你實際通知用戶的代碼嗎?你是否將'contentView'分配給'Notification'對象的'contentView'? – Wroclai 2011-04-27 14:24:02

+1

您應該發佈導致崩潰的異常。 – hackbod 2011-04-27 14:26:50

+0

@ViktorLannér抱歉,我說我失去了抱歉。我已經刪除了大部分,因爲它沒有工作。但如果你能幫助我,我會非常高興! – carefacerz 2011-04-27 14:27:12

回答

0

既然你已經發布錯誤消息,這條線會告訴你爲什麼你沒有成功通過定製通知:

引起:android.view.InflateException:二進制XML文件線#1:類不允許被充氣android.widget.Horizo​​ntalScrollView

卸下HorizontalScrollView和只是簡單TextView S/ImageView氏試驗。

+0

謝謝,但它沒有工作:( – carefacerz 2011-04-27 14:43:35

+0

你能發佈LogCat消息嗎?紅色標記的文本會告訴你錯誤被檢測到的位置 – Wroclai 2011-04-27 14:44:46

+0

我該如何複製它? – carefacerz 2011-04-27 14:50:52