Ajout de bouton d'action en fonction de notification personnalisée
J'ai fait custom notification
et il y a un bouton qui, je veux l'exécuter deux functionalities on notification and button click
. Je regarde beaucoup de liens, mais ne pouvait pas trouver le moyen d'ajouter des boutons à l'écoute.
Peut aider quelqu'un. Voici mon code. Merci beaucoup.
private void startNotification() {
Intent intent;
PendingIntent pIntent;
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.mynotification);
Context context = getApplicationContext();
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher).setContent(
remoteViews);
if (hasFlash) {
intent = new Intent(context, FlashLight.class);
pIntent = PendingIntent.getActivity(context, 1, intent, 0);
} else {
intent = new Intent(context, BlankWhiteActivity.class);
pIntent = PendingIntent.getActivity(context, 1, intent, 0);
}
builder.setContentIntent(pIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = builder.setContentTitle("Flashlight")
.setContentText("Lighten your world!!!").build();
mNotificationManager.notify(1, notif);
remoteViews.setOnClickPendingIntent(R.id.closeOnFlash, pIntent);
}
J'ai passé le bouton id (closeOnFlash
) dans setOnClickPendingIntent
ne sais pas pourquoi sa ne fonctionne pas.
Et voici mon xml
:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="@+id/notifiation_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:contentDescription="@string/appImage"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:text="@string/flashLightOn"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/closeOnFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="@string/close" />
- Peut-être ce fil permet de? stackoverflow.com/questions/5479165/...
- Ce fil devrait répondre à ce que vous essayez de faire: stackoverflow.com/questions/12438209/...
- Ok merci beaucoup je vais lui donner un look.
- En fait le second a vraiment fonctionné pour moi sur mon HTC sensation, havent vérifié les problèmes de compatibilité.
Vous devez vous connecter pour publier un commentaire.
Début de la notification:
xml utilisé:
Dans le manifeste en vertu de l'Application de la balise: