简体   繁体   English

状态栏面板中的 Android 通知工具栏?

[英]Android Notification Toolbar in the Status bar panel?

I was wondering how I could implement a fixed toolbar in the android Status bar of any android device.我想知道如何在任何 android 设备的 android 状态栏中实现一个固定的工具栏。 I am talking about the buttons in the notification displayed below.我说的是下面显示的通知中的按钮。 An example is provided below.下面提供了一个示例。 Is it possible have this running even when the user hasn't opened the application?即使用户还没有打开应用程序,它是否有可能运行?

Can someone point me in the right direction please?有人可以指出我正确的方向吗? Is there a library or can we implement this using the native android libraries provided?是否有库,或者我们可以使用提供的本机 android 库来实现它吗?

在此处输入图片说明

As a simple example, the following code will issue at boot an ongoing Notification with a Button to launch your app.作为一个简单的例子,下面的代码将在启动时发出一个带有Button的持续Notification来启动你的应用程序。

First, in your manifest, request the permission to get the BOOT_COMPLETED broadcast, and register a Receiver to handle it.首先,在您的清单中,请求获得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>

The BootReceiver simply issues the Notification using a static method which is defined in MainActivity for this example. BootReceiver简单地使用在MainActivity定义的static方法发出Notification

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

The setNotification() method creates a RemoteViews instance for the Notification using the simple layout below, and sets a PendingIntent on the Button with the launch Intent for your app. 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);
    }
}

The Notification 's layout is simply an ImageView and a Button in a horizontal LinearLayout . Notification的布局只是一个ImageView和一个水平LinearLayoutButton

<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>

Please note that, since API 3.1, you will have to launch your app at least once after installation to bring it out of the stopped state.请注意,从 API 3.1 开始,您必须在安装后至少启动一次您的应用程序才能使其脱离停止状态。 Until then, the BootReceiver will not be delivered the broadcast.在此之前, BootReceiver将不会传送广播。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM