简体   繁体   中英

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

在此处输入图片说明

As a simple example, the following code will issue at boot an ongoing Notification with a Button to launch your app.

First, in your manifest, request the permission to get the BOOT_COMPLETED broadcast, and register a Receiver to handle it.

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

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.

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 .

<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. Until then, the BootReceiver will not be delivered the broadcast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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