带有按钮的Android通知

dor*_*nsl 16 android android-notifications

我正在尝试用2个按钮发出通知:

  • 一个人带我回到活动
  • 另一个关闭它

有没有人知道如何捕捉按钮点击事件(请记住活动暂停)?

LiT*_*Tle 42

我很高兴发布它!整晚工作后我发现了一些东西.所以,我们走吧!

1.为通知创建xml布局文件.

2.使用Notification.Builder创建通知.添加所需内容(图标,声音等)后,执行以下操作:

        //R.layout.notification_layout is from step 1

        RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.notification_layout);

        setListeners(contentView);//look at step 3

        notification.contentView = contentView;
Run Code Online (Sandbox Code Playgroud)

3.创建方法setListeners.在这个方法里面你必须写这个:

    //HelperActivity will be shown at step 4

    Intent radio=new Intent(ctx, packagename.youractivity.class);  
    radio.putExtra("AN_ACTION", "do");//if necessary

    PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0);
    //R.id.radio is a button from the layout which is created at step 2  view.setOnClickPendingIntent(R.id.radio, pRadio); 

    //Follows exactly my code!
    Intent volume=new Intent(ctx, tsapalos11598712.bill3050.shortcuts.helper.HelperActivity.class);
    volume.putExtra("DO", "volume");</p>

    //HERE is the whole trick. Look at pVolume. I used 1 instead of 0.
    PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0);
    view.setOnClickPendingIntent(R.id.volume, pVolume);
Run Code Online (Sandbox Code Playgroud)

4.根据我的要求,我使用了一个响应意图的HelperActivity.但对你来说,我认为没有必要.

如果你想要完整的源代码,你可以浏览它或从我的git repo下载它.该代码仅供个人使用,因此不要期望阅读带有大量注释的华丽代码.https://github.com/BILLyTheLiTTle/AndroidProject_Shortcuts

以上所有,回答了从不同按钮捕捉事件的问题.

关于取消通知我在这里重定向你

如何在Android中清除通知

只需记住在第一次调用通知时使用您在notify方法中解析的ID

  • 这在ICS 4.0.4上非常出色!您应该考虑正确格式化答案,以便其他用户可以很好地了解您的答案. (8认同)
  • 你是对的.我应该从开始做到这一点.我希望现在好一些.由于您的评论,我添加了下载源代码的链接.从现在开始,所有的读者都应该感谢你,而不是我.快乐的结局年和最快乐的新年 (8认同)

Ami*_*min 5

这里有一个完整的例子

    //Add this code to onCreate or some onclick Buttton
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
    long when = System.currentTimeMillis();
    builder.setSmallIcon(R.drawable.ic_notification);
    Intent notificationIntent = new Intent(getApplicationContext(), notificationActivity.class).putExtra("notification", "1");
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    Notification notification = builder.getNotification();
    notification.when = when;

    RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_view);
    remoteViews.setTextViewText(R.id.tvName, "New Name");
    listener(remoteViews,getApplicationContext());


    notification.contentView = remoteViews;
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    manager.notify(1, notification);
Run Code Online (Sandbox Code Playgroud)

然后你可以定义监听器方法:

    public void listener(RemoteViews remoteViews, Context context) {
    // you have to make intetns for each action (your Buttons)
    Intent intent = new Intent("Accept");
    Intent intent2 = new Intent("Reject");

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,1,intent,0);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,1,intent2,0);

    // add actions here !
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("Accept");
    intentFilter.addAction("Reject");


    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("Accept")){
                Toast.makeText(context, "Accepted !!", Toast.LENGTH_SHORT).show();
            } else if(intent.getAction().equals("Reject")) {
                Toast.makeText(context, "Rejected !!", Toast.LENGTH_SHORT).show();
            }
        }
    };

    context.registerReceiver(receiver,intentFilter);
    remoteViews.setOnClickPendingIntent(R.id.ivRequest,pendingIntent);
    remoteViews.setOnClickPendingIntent(R.id.ivReject,pendingIntent2);

}
Run Code Online (Sandbox Code Playgroud)

这是用于装饰您的通知的 notification_view 布局。

    <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:text="Request from "
    />

<TextView
    android:id="@+id/tvName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginStart="15dp"
    android:layout_toRightOf="@id/textView"
    android:text="Amin"
    />

<ImageView
    android:id="@+id/ivRequest"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignParentEnd="true"
    android:layout_centerVertical="true"
    android:src="@drawable/notification"
    />

<ImageView
    android:id="@+id/ivReject"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginEnd="10dp"
    android:layout_toLeftOf="@id/ivRequest"
    android:layout_centerVertical="true"
    android:src="@drawable/trash"
    />

  </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)


Shi*_*ine 4

至于 ICS,这个问题很容易回答,因为所需的行为反映了默认通知:您可以通过向右滑动来关闭通知,并且您可以定义当用户按下它时将其发送到哪个活动PendingIntent,只需使用:

// The PendingIntent to launch our activity if the user selects this
// notification.  Note the use of FLAG_CANCEL_CURRENT so that, if there
// is already an active matching pending intent, cancel it and replace
// it with the new array of Intents.
PendingIntent contentIntent = PendingIntent.getActivities(this, 0,
        makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)

代码取自http://developer.android.com/guide/topics/ui/notifiers/notifications.html