【问题标题】:Android Notification Toolbar in the Status bar panel?状态栏面板中的Android通知工具栏?
【发布时间】:2016-04-29 13:06:11
【问题描述】:

我想知道如何在任何 android 设备的 android 状态栏中实现固定工具栏。 我说的是下面显示的通知中的按钮。下面提供了一个示例。即使用户没有打开应用程序,它是否可能运行?

有人能指出我正确的方向吗?是否有库或者我们可以使用提供的原生 android 库来实现它吗?

【问题讨论】:

  • 您可以简单地使用在引导接收器中启动的自定义View 创建一个正在进行的Notification。不需要图书馆。
  • 你能给我一个例子或参考链接吗? :) @MikeM。

标签: java android android-studio android-notifications android-statusbar


【解决方案1】:

作为一个简单的示例,以下代码将在启动时发出一个正在进行的Notification 和一个Button 来启动您的应用程序。

首先,在您的清单中,请求获取BOOT_COMPLETED 广播的权限,并注册一个接收器来处理它。

<manifest ...>

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    ...

    <application ...>
        ...

        <receiver android:name=".BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

BootReceiver 只是使用static 方法发出Notification,此示例在MainActivity 中定义。

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        MainActivity.setNotification(context, true);
    }
}

setNotification() 方法使用下面的简单布局为Notification 创建一个RemoteViews 实例,并在Button 上设置一个PendingIntent 并为您的应用启动Intent。

public static void setNotification(Context context, boolean enabled) {
    NotificationManager manager =
        (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

    if (enabled) {
        final RemoteViews rViews = new RemoteViews(context.getPackageName(),
                                                   R.layout.notification);

        final Intent intent = context.getPackageManager()
            .getLaunchIntentForPackage(context.getPackageName());

        if (intent != null) {
            PendingIntent pi = PendingIntent.getActivity(context,
                                                         0,
                                                         intent,
                                                         0);

            rViews.setOnClickPendingIntent(R.id.notification_button_1, pi); 
        }

        Notification.Builder builder = new Notification.Builder(context);
        builder.setContent(rViews)
            .setSmallIcon(R.drawable.ic_launcher)
            .setWhen(0)
            .setOngoing(true);

        manager.notify(0, builder.build());
    }
    else {
        manager.cancel(0);
    }
}

Notification 的布局只是一个ImageView 和一个水平Button 中的LinearLayout。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView android:id="@+id/notification_image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_launcher" />

    <Button android:id="@+id/notification_button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Button" />

</LinearLayout>

请注意,自 API 3.1 起,您必须在安装后至少启动一次应用程序才能使其退出 已停止 状态。在此之前,BootReceiver 不会被发送广播。

【讨论】:

  • 我认为“当用户尚未打开应用程序时”表示您希望它不断显示,即使用户在当前会话期间尚未运行您的应用程序。这就是引导接收器负责的工作。如果您安装应用程序,运行一次,然后重新启动,它会在启动过程完成后立即显示。可能需要一点时间。
  • 不,您不需要在使用新版本更新之前卸载,但有时如果您更改布局,IDE 无法更新布局中的 XML。您可能需要清理,然后重建项目。此外,只有有限的Views 子集可以放入Notifications,因此您只能使用这些子集。给我一秒钟,我会为你找到清单。
  • 你是对的。 “视图”导致了这个问题。修复它:)
  • 没问题。是的,我不确定他们为什么不允许这样做,但事实就是这样。
  • @MIKEPERETZ 我回答的第一个代码块中的第三行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 1970-01-01
相关资源
最近更新 更多