3

我想知道如何创建自定义通知并编辑布局以编程方式添加视图以允许用户自定义通知。

我不能做的是:

如果我像这样创建自定义 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);

非常感谢...

4

2 回答 2

3

通知区域的使用RemoteViews就像 Android App Widgets。

它们不像您的应用程序中的 UI 部分。您可以参考它们,您可以创建它们,但它们驻留在其他应用程序中。(通知的 UI 在系统应用中,应用小部件在 Home Launcher 应用中。)

由于它们并未真正显示在您的应用中,因此您无法直接访问它们。例如,您不能设置onClickListener

这就是为什么我们有RemoteViews.

使用它们,您可以通过提供布局文件和一些内置函数来更改文本、图像等来定义如何创建它们。并且您PendingIntent可以在单击按钮时触发它们。而已。

最后,您实际上可以动态更改通知中的 UI,但不是您通常在应用程序中执行的方式。

您可以查看此答案以了解如何创建一个。 https://stackoverflow.com/a/21283668/1016462

于 2014-12-30T08:28:52.887 回答
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 .. n 放置操作 ID。它看起来像刷新,因为我们重新创建了新的通知栏。这只是目前 100% 的方法!如果有人认为不同,请发给我应用示例的链接。

您可以使 Activity 对象也 public static 。

现在很容易控制任何组合:

服务 - 活动 服务 - 广播接收器 广播接收器 - 活动

但!:

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

...

setImageViewResource 不能将此行用于通知或任何其他样式更改方法。

唯一的方法是创建服务,放入 startCommand 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();
    }
}

...

于 2017-01-19T14:08:49.673 回答