Issue
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?
Solution
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.
Answered By - Mike M.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.