2

我想知道如何創建一個自定義通知和編輯佈局添加視圖以編程方式允許用戶自定義通知。Android:如何創建自定義通知並編輯其佈局以編程方式添加視圖?

什麼我不能做的是這樣的:

如果我創建一個自定義的LinearLayout是這樣的:

LinearLayout ll = new LinearLayout(c); 
ImageView iv = new ImageView(c); 
iv.setImageResource(R.drawable.bt_close); 
ll.addView(iv); 

我如何使用此佈局創建一個通知,並添加一個動作,當我做點擊ImageView?

的代碼,以使自定義通知是僅此?

Notification notification = (statusIcon, appName, System.currentTimeMillis()); 
notification.contentView = new RemoteViews(this.getPackageName(),R.layout.notification); 

非常感謝在所有...

+0

http://stackoverflow.com/questions/21237495/create -custom-大的通知 – duggu 2014-12-30 07:27:39

回答

1

通知區域使用RemoteViews就像Android應用程序窗口小部件。

他們不喜歡你的應用程序的UI部件。你有參考,你可以創建它們,但它們駐留在其他應用程序中。 (通知UI是系統應用程序,應用小部件是在首頁桌面應用)。

因爲他們沒有真正在應用中展示的,你不能直接訪問它們。例如,您不能設置onClickListener

這就是爲什麼我們有RemoteViews

有了它們,您可以通過給佈局文件和一些內置函數來更改文本,圖像等來定義如何創建它們。單擊按鈕時會觸發PendingIntent。而已。

最後,實際上你可以更改UI在通知動態地而不是你通常做在您的應用程序的方式。

你可以看到這個答案如何創建一個。 https://stackoverflow.com/a/21283668/1016462

0

通知風格,你可以改變它只是重新創建一個新的風格的通知。它的成本很小(如刷新)。但工作。

使您的對象成爲公共靜態的第一步。

實施例:

public static NotificationCompat.Builder mBuilder; 
public static RemoteViews contentBigCustom; 
public static RemoteViews contentSmallNotExpand; 
public static RemoteViews contentBigCustomFROMCODE; 

下一個 - 使這條線:

notification.contentView = contentBigCustom;

來的樣子:

// in top 
public static RemoteViews contentAny; 

也許你正在使用onStartCommand初始對象全髖關節置換好...

if (IWANTNOTIFYSTYLE == 0) { 
contentBigCustom = RemoteViews(this.getPackageName(),R.layout.notificationBig); 
} 
else { 
contentBigCustomFROMCODE = RemoteViews(this.getPackageName(),R.layout.notificationBig); 
} 

contentSmallNotExpand = RemoteViews(this.getPackageName(),R.layout.notificationSmall); 

contentBigCustomFROMCODE = RemoteViews(this.getPackageName(),R.layout 。notificationBig);

mBuilder = new NotificationCompat.Builder(this); 
     mBuilder.setDefaults(Notification.DEFAULT_ALL); 
     mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); 
     mBuilder.setSmallIcon(R.drawable.play); 
     mBuilder.setContentText("This text is not in function but its needed to create notify"); 
     mBuilder.setLargeIcon(icon); 
     mBuilder.setPriority(Notification.PRIORITY_MAX); 
     mBuilder.setContent(contentSmallNotExpand); 
     mBuilder.setCustomBigContentView(contentBigCustom); 

    After notify exe line :  

mNotificationManager.notify(0,mBuilder.build());

put : 

stopSelf() 

過度殺死服務。當你想要改變樣式再次啓動服務,並把樣式1 2的行動ID .. .. 它看起來像刷新,因爲我們重新創建新的通知欄。 現在這只是100%的方法! 如果有人認爲不同,請張貼我鏈接到應用程序的例子。

您可以使Activity對象也是靜態的。

它現在是很容易控制的任意組合:

服務 - 活動 服務 - 廣播接收器 廣播接收器 - 活動

但是! :

Service1.contentNotify.setImageViewResource(R.id.NOTYFY_BTN,R.drawable.stop);

...

setImageViewResource不能使用此行的通知或任何其他樣式改變方法。

唯一的方法是創建服務,換上啓動命令CREATE_NOTIFY() 從notify.xml要加載什麼意圖動作名稱查找。 通知後生成並添加貼心的服務,然後從廣播呼叫:

Intent serviceIntent = new Intent("WITHSTOP"); 

    serviceIntent.putExtra("WITHSTOP", "1"); 
    // must be MyApp extend Application reason : from broadcaster you cant access getContext and startService etc.    
    MyApp.getInstance().startService(serviceIntent); 

的樣子:

public class MyApp extends Application { 
    private static MyApp instance; 

    public static MyApp getInstance() { 
     return instance; 
    } 

    public static Context getContext(){ 
     return instance; 
     // or return instance.getApplicationContext(); 
    } 

    @Override 
    public void onCreate() { 
     instance = this; 
     super.onCreate(); 
    } 
} 

...

相關問題