android - 自定义通知中按钮的事件 OnClick

标签 android button notifications onclick

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

//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);

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

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

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

谢谢,

西蒙娜

最佳答案

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

它会创建一个自定义通知,当用户点击按钮时它会发送一个广播。 有一个接收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");
        }
    }

AndroidManifest.xml

中注册接收器
<receiver android:name=".MainActivity$DownloadCancelReceiver" android:exported="false">
            <intent-filter>
                <action android:name="Download_Cancelled" />
            </intent-filter>
        </receiver>

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

小部件 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" >

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

关于android - 自定义通知中按钮的事件 OnClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5479165/

相关文章:

当应用程序在后台时,Android 第二次推送通知有效负载数据未接收到额外内容

java - 判断是否来自Android App的HTTP请求?然后适当回应

android - 将数据从 Activity 传递到另一个 Activity,然后传递到 ViewPager 中的 fragment

android - 从通知导航到父 Activity

android - 在关闭应用程序之前处理最后一次后退点击的最正确方法是什么

asp.net - ASP.NET 按钮周围的虚线边框

java - libdgx如何设置TextButton的样式?

Javascript提示插入pdf文件?

android - 我希望在打开应用程序后关闭通知

ios - 如何处理来自应用程序委托(delegate)的 UIViewcontroller 中的通知?