自定义通知中按钮的事件 OnClick [英] Event OnClick for a button in a custom notification

查看:28
本文介绍了自定义通知中按钮的事件 OnClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带按钮的自定义通知.要设置通知并在我的按钮上使用 OnClick 事件,我使用了以下代码:

//通知和通知的意图通知通知 = 新通知(R.drawable.stat_notify_missed_call,"自定义通知", System.currentTimeMillis());Intent mainIntent = new Intent(getBaseContext(), NotificationActivity.class);PendingIntent pendingMainIntent = PendingIntent.getActivity(getBaseContext(),0, mainIntent , 0);通知.contentIntent = pendingMainIntent;//我的按钮的远程视图和意图RemoteViews notificationView = new RemoteViews(getBaseContext().getPackageName(),R.layout.remote_view_layout);Intent activityIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:190"));PendingIntent pendingLaunchIntent = PendingIntent.getActivity(getBaseContext(), 0,activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);notificationView.setOnClickPendingIntent(R.id.button1,未决LaunchIntent);通知.contentView = 通知视图;notificationManager.notify(CUSTOM_NOTIFICATION_ID, 通知);

使用此代码,我有一个带有自定义布局的自定义通知...但我无法单击按钮!每次我尝试单击按钮时,我都会单击整个通知,因此脚本会启动mainIntent"而不是activityIntent".

我在互联网上读到此代码不适用于所有终端.我已经在模拟器和 HTC Magic 上试过了,但我总是遇到同样的问题:我无法点击按钮!

我的代码是对的吗?有人可以帮助我吗?

谢谢,

西蒙

解决方案

我正在我的 MyActivity.java 类中编写代码,该类扩展了 android.app.Activity

它创建一个自定义通知,当用户点击按钮时它会发送一个broadcast.有一个广播接收器接收broadcast.

private void createDownloadNotification() {Intent closeButton = new Intent("Download_Cancelled");closeButton.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, closeButton, 0);RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.widget_update_notification);NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setTicker("Ticker Text").setContent(notificationView);notificationView.setProgressBar(R.id.pb_progress, 100, 12, false);notificationView.setOnClickPendingIntent(R.id.btn_close, pendingSwitchIntent);NotificationManager.notify(1, builder.build());}公共静态类 DownloadCancelReceiver 扩展 BroadcastReceiver {@覆盖public void onReceive(上下文上下文,意图意图){System.out.println("收到取消的事件");}}

AndroidManifest.xml

中注册接收器

<意图过滤器><action android:name="Download_Cancelled"/></意图过滤器></接收器>

由于是内部类所以必须使用$符号

Widget xml 在这里

<按钮android:id="@+id/btn_close"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="关闭我"/><进度条android:id="@+id/pb_progress"style="?android:attr/progressBarStyleHorizo​​ntal"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>

I have a custom notification with a button. To set the notification and use the event OnClick on my button I've used this code:

//Notification and intent of the notification 
Notification notification = new Notification(R.drawable.stat_notify_missed_call,
            "Custom Notification", System.currentTimeMillis());

Intent mainIntent = new Intent(getBaseContext(), NotificationActivity.class);
PendingIntent pendingMainIntent = PendingIntent.getActivity(getBaseContext(),
    0, mainIntent , 0);
notification.contentIntent = pendingMainIntent;

//Remoteview and intent for my button
RemoteViews notificationView = new RemoteViews(getBaseContext().getPackageName(),
    R.layout.remote_view_layout);

Intent activityIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:190"));
PendingIntent pendingLaunchIntent = PendingIntent.getActivity(getBaseContext(), 0,
            activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

notificationView.setOnClickPendingIntent(R.id.button1,
    pendingLaunchIntent);

notification.contentView = notificationView;

notificationManager.notify(CUSTOM_NOTIFICATION_ID, notification);

With this code I've a custom notification with my custom layout...but I can't click the button! every time I try to click the button I click the entire notification and so the script launch the "mainIntent" instead of "activityIntent".

I have read in internet that this code doesn't work on all terminals. I have tried it on the emulator and on an HTC Magic but I have always the same problem: I can't click the button!

My code is right? someone can help me?

Thanks,

Simone

解决方案

I am writing code in my MyActivity.java class that extends android.app.Activity

It creates a custom notification, when user click on the button it sends a broadcast. There is a broadcast receiver that receives the broadcast.

private void createDownloadNotification() {
        Intent closeButton = new Intent("Download_Cancelled");
        closeButton.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, closeButton, 0);

        RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.widget_update_notification);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setTicker("Ticker Text").setContent(notificationView);
        notificationView.setProgressBar(R.id.pb_progress, 100, 12, false);
        notificationView.setOnClickPendingIntent(R.id.btn_close, pendingSwitchIntent);

        notificationManager.notify(1, builder.build());

    }


public static class DownloadCancelReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            System.out.println("Received Cancelled Event");
        }
    }

Register receiver in AndroidManifest.xml

<receiver android:name=".MainActivity$DownloadCancelReceiver" android:exported="false">
            <intent-filter>
                <action android:name="Download_Cancelled" />
            </intent-filter>
        </receiver>

Since it is inner class so have to use $ sign

Widget xml is here

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

    <Button
        android:id="@+id/btn_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close Me" />

    <ProgressBar
        android:id="@+id/pb_progress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

这篇关于自定义通知中按钮的事件 OnClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆