gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:03:11 25 4
gpt4 key购买 nike

我有一个带有按钮的自定义通知。要设置通知并在我的按钮上使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com