Android开发教程:Notification和NotificationManager畅聊
发布时间:2021-11-24 16:16:22 所属栏目:教程 来源:互联网
导读:Notification和NotificationManager操作相对比较简单,一般获取系统级的服务NotificationManager,然后实例化Notification,设置它的属性,通过NotificationManager发出通知就可以了。基本步骤如下: 1.获取NotificationManager String service = Context.NO
Notification和NotificationManager操作相对比较简单,一般获取系统级的服务NotificationManager,然后实例化Notification,设置它的属性,通过NotificationManager发出通知就可以了。基本步骤如下: 1.获取NotificationManager String service = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager =(NotificationManager)getSystemService(service); 2.实例化Notification对象 //实例化Notification Notification notification = new Notification(); 3.设置Notification的属性 // 设置显示图标,该图标会在状态栏显示 int icon = notification.icon = R.drawable.happy; // 设置显示提示信息,该信息也在状态栏显示 String tickerText = "测试Notification"; // 显示时间 long when = System.currentTimeMillis(); notification.icon = icon; notification.tickerText = tickerText; notification.when = when; //也可以这样设置 Notification notification_2=new Notification(icon,tickerText,when) 调用setLatestEventInfo()方法在视图中设置图标和时间。 // 实例化Intent Intent intent = new Intent(MainActivity.this, MainActivity.class); // 获得PendingIntent PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); // 设置事件信息 notification.setLatestEventInfo(MainActivity.this, " Title", "Content", pIntent); 4.发出通知 //Notification标示ID private static final int ID = 1; //发出通知 mNotificationManager.notify(ID, n); 下面是具体的例子,在这个例子里定义了一个MainActivity发出广播通知,定义一个MyReceiver类继承Broadcasts接受通知,当接收完通知之后,启动一个SecondActivity,在SecondActivity类中通过Notification和NotificationManager来可视化显示广播通知。具体的步骤如下: MainActivity.java package com.Android.notification; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { // 声明Button private Button btn; // 定义Broadcast Receiver action private static final String MY_ACTION = "com.android.notification.MY_ACTION"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置当前布局视图 setContentView(R.layout.main); // 实例化Button btn = (Button)findViewById(R.id.Button1); // 添加事件监听器 btn.setOnClickListener(listener); } // 创建事件监听器 private OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { // 实例化Intent Intent intent = new Intent(); // 设置Intent action属性 intent.setAction(MY_ACTION); // 发起广播 sendBroadcast(intent); } }; } MyReceiver.java package com.android.notification; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // 实例化Intent Intent i = new Intent(); // 在新的任务中启动Activity i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 设置Intent启动的组件名称 i.setClass(context, SecondActivity.class); // 启动Activity显示通知 context.startActivity(i); } } SecondActivity.java package com.android.notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SecondActivity extends Activity { // 声明按钮 private Button cancelBtn; // 声明Notification private Notification notification ; // 声明NotificationManager private NotificationManager mNotification; // Notification标示ID private static final int ID = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); // 实例化按钮 cancelBtn = (Button)findViewById(R.id.cancelButton2); // 获得NotificationManager实例 String service = NOTIFICATION_SERVICE; mNotification = (NotificationManager)getSystemService(service); // 实例化Notification notification = new Notification(); // 设置显示图标,该图标会在状态栏显示 int icon = notification.icon = android.R.drawable.stat_notify_chat; // 设置显示提示信息,该信息也会在状态栏显示 String tickerText = "Test Notification"; // 显示时间 long when = System.currentTimeMillis(); notification.icon = icon; notification.tickerText = tickerText; notification.when = when; // 实例化Intent Intent intent = new Intent(this, MainActivity.class); // 获得PendingIntent PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); // 设置事件信息 notification.setLatestEventInfo(this, "消息", "Hello Android", pi); // 发出通知 mNotification.notify(ID, notification); // 为按钮添加监听器 cancelBtn.setOnClickListener(cancelListener); } // 取消通知监听器 private OnClickListener cancelListener = new OnClickListener() { @Override public void onClick(View v) { // 取消通知 mNotification.cancel(ID); } }; } ![]() (编辑:开发网_开封站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |