IMAGE 我有代码。工作正常,也关闭通知。但当我点击"解雇"按钮,然后打开应用程序并关闭对话框。这不是我要求的。实际上我想要,点击" DISMISS"后关闭通知对话框按钮没有打开任何活动。
MainActivity.java
public class MainActivity extends Activity{
static int numMessages = 0;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notificationmain);
context = getApplicationContext();
Button bcustomnotifyaction = (Button) findViewById(R.id.customnotificationaction);
bcustomnotifyaction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomNotificationAction();
}
});
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void CustomNotificationAction() {
// Set Notification Title
String strtitle = "You Got New Notification.";
// Set Notification Text
String strtext ="Hi,How are You?";
// Open NotificationView Class on Notification Click
Intent intent = new Intent(this, NotificationView.class);
// Send data to NotificationView Class
intent.putExtra("title", strtitle);
intent.putExtra("text", strtext);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
int notificationId = new Random().nextInt(); // just use a counter in some util class...
PendingIntent dismissIntent = NotificationActivity.getDismissIntent(notificationId, getApplicationContext());
//Create Notification using NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
// Set Icon
.setSmallIcon(R.drawable.logosmall)
// Set Ticker Message
.setTicker(strtitle)
// Set Title
.setContentTitle(strtitle)
// Set Text
.setContentText(strtext)
// Add an Action Button below Notification
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Dismiss", dismissIntent)
.addAction(android.R.drawable.ic_menu_info_details, "Info", pIntent)
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// showing action button on notification
.setPriority(Notification.PRIORITY_MAX)
.setWhen(0)
/* Increase notification number every time a new notification arrives */
.setNumber(++numMessages)
// Dismiss Notification
.setAutoCancel(true);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(0, builder.build());
}
}
notificationmain.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NotificationView" >
<Button
android:id="@+id/customnotificationaction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/customnotificationaction" />
</RelativeLayout>
NotificationView.java
public class NotificationView extends Activity {
String title;
String text;
TextView txttitle;
TextView txttext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notificationview);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Dismiss Notification
notificationmanager.cancel(0);
// Retrive the data from MainActivity.java
Intent i = getIntent();
title = i.getStringExtra("title");
text = i.getStringExtra("text");
// Locate the TextView
txttitle = (TextView) findViewById(R.id.title);
txttext = (TextView) findViewById(R.id.text);
// Set the data into TextView
txttitle.setText(title);
txttext.setText(text);
}
}
notificationview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/lbltitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lbltitle" />
<TextView
android:id="@+id/lbltext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lbltitle"
android:text="@string/lbltext" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/lbltitle" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/lbltext" />
</RelativeLayout>
NotificationActivity.java
public class NotificationActivity extends Activity {
public static final String NOTIFICATION_ID = "NOTIFICATION_ID";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(0);
finish(); // since finish() is called in onCreate(), onDestroy() will be called immediately
}
public static PendingIntent getDismissIntent(int notificationId, Context context) {
Intent intent = new Intent(context, NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(NOTIFICATION_ID, notificationId);
PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
return dismissIntent;
}
}
解决方案:
我使用广播接收器获得了解决方案。
我改变了代码: 我在 MainActivity.java CustomNotificationAction()中添加了此代码。
//Create an Intent for the BroadcastReceiver
Intent buttonIntent = new Intent(context, ButtonReceiver.class);
//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent,0);
并更改此行
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Dismiss", dismissIntent)
致
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Dismiss", btPendingIntent)
并添加了一个Java文件
ButtonReceiver.java
public class ButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Notification Dialog Closed",
Toast.LENGTH_LONG).show();
Log.d("Notification:","Notification Dialog Closed");
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(0);
PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
NotificationCompat.Builder mb = new NotificationCompat.Builder(context);
mb.setContentIntent(resultPendingIntent);
}
}
在Mainfest文件中注册广播
<receiver android:name=".ButtonReceiver"/>
我的问题解决了:)
您也可以从以下链接下载源代码:
答案 0 :(得分:1)
应设置广播接收器以检测您的动作点击而不启动活动。
Dismiss Ongoing Android Notification Via Action Button Without Opening App
答案 1 :(得分:1)
你可以用服务和广播接收器来做,但我认为广播接收器是更好的选择。
int final MY_NOTIFICATION_ID = 1;
//Create an Intent for the BroadcastReceiver
Intent buttonIntent = new Intent(context, AutoDismissReceiver.class);
buttonIntent.putExtra("notificationId",MY_NOTIFICATION_ID);
//Create the PendingIntent
PendingIntent btPendingIntent = PendingIntent.getBroadcast(context, 0, buttonIntent,0);
//Pass this PendingIntent to addAction method of Intent Builder
NotificationCompat.Builder mb = new NotificationCompat.Builder(getBaseContext());
.....
.....
mb.addAction(R.drawable.ic_dismiss, "Dismiss Action", btPendingIntent);
manager.notify(MY_NOTIFICATION_ID, mb.build());
以上功能的广播接收器
public class AutoDismissReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int notificationId = intent.getIntExtra("notificationId", 0);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notificationId);
}
}
希望这会对你有所帮助。