温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Notification(通知栏)中自定义控件的点击事件

发布时间:2020-07-19 06:10:20 来源:网络 阅读:4534 作者:有你有我lin 栏目:移动开发

package com.example.customnotification;


import android.os.Bundle;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.widget.RemoteViews;

import android.widget.Toast;


public class MainActivity extends Activity

{


   private NotificationManager manager;

   private Notification mNotify;

   private RemoteViews remoteViews;

   private MyBroadCast receiver;

private static String ACTION="a";

   @Override

   protected void onCreate(Bundle savedInstanceState)

   {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


// 注册广播,并设置过滤条件

       receiver = new MyBroadCast();

       IntentFilter filter=new IntentFilter("a");

       registerReceiver(receiver, filter);

   }


   @Override

   public boolean onCreateOptionsMenu(Menu menu)

   {

       // Inflate the menu; this adds items to the action bar if it is present.

       getMenuInflater().inflate(R.menu.main, menu);


       return true;

   }


   @SuppressWarnings("deprecation")

   public void notify(View v){

       if(mNotify==null){

           mNotify = new Notification(R.drawable.ic_launcher, "酷狗音乐",

                   System.currentTimeMillis());

           Intent intent = new Intent(this, SecondActivity.class);

           PendingIntent pendingIntent = PendingIntent.getActivity(this,123,

                   intent, PendingIntent.FLAG_UPDATE_CURRENT);//默认整个布局的点击事件

//自定布局  notify_music.xml

Notification(通知栏)中自定义控件的点击事件



       remoteViews = new RemoteViews(getPackageName(), R.layout.notify_music);


       Intent action=new Intent("a");

       remoteViews.setOnClickPendingIntent(R.id.notify_music_play_imgbtn,

PendingIntent.getBroadcast(MainActivity.this, 11, action,PendingIntent.FLAG_UPDATE_CURRENT));        //播放按钮的点击事件


          mNotify.contentView=remoteViews;        // 实现自定义布局和 布局事件

           mNotify.contentIntent=pendingIntent;

       }

       manager.notify(0, mNotify);


   }


   class MyBroadCast extends BroadcastReceiver{


       @Override

       public void onReceive(Context context, Intent intent)

       {

           Toast.makeText(MainActivity.this, "11111111111111",              Toast.LENGTH_LONG).show();          

           Log.e("我的官博", "按钮点击事件");

           startActivity(new Intent(MainActivity.this,ThirdActivity.class));

       }


   }


   @Override

   protected void onStop()

   {

       // TODO Auto-generated method stub

       super.onStop();

       unregisterReceiver(receiver);

   }


}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI