失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > android通知栏如何添加按钮 如何在通知栏上放置媒体控制器按钮?

android通知栏如何添加按钮 如何在通知栏上放置媒体控制器按钮?

时间:2020-07-29 14:22:20

相关推荐

android通知栏如何添加按钮 如何在通知栏上放置媒体控制器按钮?

要在您的应用中获取媒体播放器控制器,只需按照:

在MainActivity中调用此方法

public void showNotification(View view){ new MyNotification(this); finish(); }

创建一个新的MynotificationClass

public class MyNotification extends Notification { private Context ctx; private NotificationManager mNotificationManager; @SuppressLint("NewApi") public MyNotification(Context ctx){ super(); this.ctx=ctx; String ns = Context.NOTIFICATION_SERVICE; mNotificationManager = (NotificationManager) ctx.getSystemService(ns); CharSequence tickerText = "Shortcuts"; long when = System.currentTimeMillis(); Notification.Builder builder = new Notification.Builder(ctx); @SuppressWarnings("deprecation") Notification notification=builder.getNotification(); notification.when=when; notification.tickerText=tickerText; notification.icon=R.drawable.ic_launcher; RemoteViews contentView=new RemoteViews(ctx.getPackageName(), R.layout.messageview); //set the button listeners setListeners(contentView); notification.contentView = contentView; notification.flags |= Notification.FLAG_ONGOING_EVENT; CharSequence contentTitle = "From Shortcuts"; mNotificationManager.notify(548853, notification); } public void setListeners(RemoteViews view){ //radio listener Intent radio=new Intent(ctx,HelperActivity.class); radio.putExtra("DO", "radio"); PendingIntent pRadio = PendingIntent.getActivity(ctx, 0, radio, 0); view.setOnClickPendingIntent(R.id.radio, pRadio); //volume listener Intent volume=new Intent(ctx, HelperActivity.class); volume.putExtra("DO", "volume"); PendingIntent pVolume = PendingIntent.getActivity(ctx, 1, volume, 0); view.setOnClickPendingIntent(R.id.volume, pVolume); //reboot listener Intent reboot=new Intent(ctx, HelperActivity.class); reboot.putExtra("DO", "reboot"); PendingIntent pReboot = PendingIntent.getActivity(ctx, 5, reboot, 0); view.setOnClickPendingIntent(R.id.reboot, pReboot); //top listener Intent top=new Intent(ctx, HelperActivity.class); top.putExtra("DO", "top"); PendingIntent pTop = PendingIntent.getActivity(ctx, 3, top, 0); view.setOnClickPendingIntent(R.id.top, pTop);*/ //app listener Intent app=new Intent(ctx, com.example.demo.HelperActivity.class); app.putExtra("DO", "app"); PendingIntent pApp = PendingIntent.getActivity(ctx, 4, app, 0); view.setOnClickPendingIntent(R.id.btn1, pApp); } }

创建一个HelperActivity类

public class HelperActivity extends Activity { private HelperActivity ctx; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); ctx = this; String action = (String) getIntent().getExtras().get("DO"); if (action.equals("radio")) { //Your code } else if (action.equals("volume")) { //Your code } else if (action.equals("reboot")) { //Your code } else if (action.equals("top")) { //Your code } else if (action.equals("app")) { //Your code } if (!action.equals("reboot")) finish(); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }

Notificationlayout.xml的XML布局

如果觉得《android通知栏如何添加按钮 如何在通知栏上放置媒体控制器按钮?》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。