我正在尝试制作屏幕调光器应用程序,它只具有开/关功能,而且通知也是如此。 如何使用.addAction实现pendingintent的取消服务?
我可以通过Service.slass中的stopSelf()停止服务,而不是从MainActivity停止服务但是如何在通知中实现它?
PendingIntent pi = PendingIntent.getService(MainActivity.this, 0, new Intent(MainActivity.this, MainService.class), 0);
final Notification notification = new
NotificationCompat.Builder(MainActivity.this)
.setTicker("Transparent Lock")
.setSmallIcon(R.drawable.ic_blur_on_black_24dp)
.setContentIntent(pi)
.setAutoCancel(false)
//.addAction(R.drawable.ic_blur_on_black_24dp, "Cancel", ????? )
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setOngoing(true)
.build();
final NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
答案 0 :(得分:0)
下面给出了工作代码,
1)创建通知的功能
调用此函数以创建通知。标题,正文,上下文,notificationsId这些都是我按照我的要求传递的所有参数,如果你不想要,你可以删除它。
public void createBigNotificationAcceptOrRejectAlert() {
final int noId= new Random().nextInt(61) + 20;
NotificationCompat.BigPictureStyle notifystyle = new NotificationCompat.BigPictureStyle();
RemoteViews contentView =
new RemoteViews(context.getPackageName(), R.layout.layout_big_notification);//using a custom layout
contentView.setTextViewText(R.id.title, "Title");
contentView.setTextViewText(R.id.desc, "Message");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context).setSmallIcon( R.drawable.ic_blur_on_black_24dp)
.setStyle(notifystyle)
.setCustomBigContentView(contentView)
.setContentTitle("Title")
.setTicker("Transparent Lock")
.setAutoCancel(true)
.setContentText(body);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBuilder.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
}
setListeners(contentView, String.valueOf(noId));//
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent =
PendingIntent.getActivity(this, noId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
Notification notification = mBuilder.build();
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
mNotificationManager.notify(noId, notification);
}
2)听取按钮的功能点击,
这是一个用于为按钮提供监听器的函数,这个函数是从上面的函数调用的。
public void setListeners(RemoteViews view, String noId)
Intent radio =
new Intent(getBaseContext(), NotificationApproveOrReject.class);
radio.putExtra("DO", "tvConfirm");
radio.putExtra("noId", noId);
PendingIntent pRadio = PendingIntent.getActivity(getBaseContext(), Integer.parseInt(noId), radio, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btAccept, pRadio);
Intent volume =
new Intent(getBaseContext(), NotificationApproveOrReject.class);
volume.putExtra("DO", "tvCancel");
volume.putExtra("noId", noId);
PendingIntent pVolume = PendingIntent.getActivity(getBaseContext(), Integer.parseInt(noId), volume, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btReject, pVolume);
}
3)处理点击的类,
最后,创建一个新类来处理点击操作。别忘了在清单中添加这个类。
public class NotificationApproveOrReject extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("Reached", "Section1");
String action = (String) getIntent().getExtras().get("DO");
final String id = (String) getIntent().getExtras().get("noId");
Log.d("RandomNumberReached", id);
if (action.equals("tvConfirm")) {
//Your action
} else if (action.equals("tvCancel")) {
//Your action
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
4)我使用layout_big_notification,
的布局<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:background="#fff"
android:orientation="vertical"
card_view:cardCornerRadius="5dp"
card_view:cardUseCompatPadding="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:adjustViewBounds="true"
android:background="@null"
android:padding="10dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:paddingEnd="@dimen/dimen10"
android:paddingTop="@dimen/dimen10"
android:textColor="#ccc"
android:textSize="@dimen/textSize14"
tools:text="Test"
/>
<TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/transparent"
android:paddingEnd="@dimen/dimen10"
android:paddingTop="@dimen/dimen1"
android:textColor="#ccc"
android:textSize="@dimen/textSize12"
tools:text="Test"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="end"
android:orientation="horizontal"
>
<Button
android:id="@+id/btAccept"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="@dimen/dimen5"
android:background="@color/colorPrimary"
android:paddingEnd="@dimen/dimen10"
android:paddingStart="@dimen/dimen10"
android:text="@string/accept"
android:textColor="@color/white"
android:textSize="@dimen/textSize14"
/>
<Button
android:id="@+id/btReject"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="@dimen/dimen5"
android:background="@color/colorPrimary"
android:paddingEnd="@dimen/dimen10"
android:paddingStart="@dimen/dimen10"
android:text="@string/reject"
android:textColor="@color/white"
android:textSize="@dimen/textSize14"
/>
</LinearLayout>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginTop="@dimen/dimen10"
android:adjustViewBounds="true"
android:contentDescription="@null"
android:scaleType="centerCrop"
/>
</LinearLayout>
希望,它可以帮到你。