温馨提示×

温馨提示×

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

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

手机通知功能Notification

发布时间:2020-07-23 05:55:57 来源:网络 阅读:372 作者:JustMetU 栏目:移动开发

Notification是Android系统中的一种通知服务,通过状态栏。手机震动、LED、提示音等多种方式提供了丰富而良好的用户体验。


一般使用步骤:


  1. 获取NotificationManager对象,调用系统NOTIFICATION_SERVICE服务,获取NotificationManager实例:NotificationManager notificationmanager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE).

  2. 创建一个Notification对象并设置Notification的各项属性。

  3. 执行通知:notificationmanager.notify().



实例如下:按钮1文字通知,按钮2声音提示,按钮3震动提示。




public class MainActivity extends Activity {


private Button textbutton,soundbutton,vibratebutton,ledbutton;

private NotificationManager nm;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textbutton=(Button)findViewById(R.id.button1);

soundbutton=(Button)findViewById(R.id.button2);

vibratebutton=(Button)findViewById(R.id.button3);

nm=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

textbutton.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

// TODO Auto-generated method stub

PendingIntent intent=PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this,MainActivity.class), 0);

//点击通知跳转到MainActivity

Notification notification=new Notification.Builder(MainActivity.this)

.setAutoCancel(true)    

.setContentTitle("文字通知")    

.setContentText("这是一个文字通知。")    

.setContentIntent(intent)    

.setSmallIcon(R.drawable.ic_launcher)    

.setWhen(System.currentTimeMillis())    

.build();

nm.notify(0,notification);

}

});

soundbutton.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {


Notification notification=new Notification();

String ringname=RingtoneManager.getActualDefaultRingtoneUri(MainActivity.this, RingtoneManager.TYPE_RINGTONE).toString();

notification.sound=Uri.parse(ringname);

nm.notify(0,notification);

}

});

vibratebutton.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {


Notification notification=new Notification();

notification.vibrate=new long[]{0,100,200,300};

nm.notify(0,notification);

}

});

}


}




xml中是3个按钮。



注意:低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。

Intent  intent = new Intent(this,MainActivity);  
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
notification.setLatestEventInfo(context, title, message, pendingIntent);          
manager.notify(id, notification);  

高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。

Notification.Builder builder = new Notification.Builder(context)  
    .setAutoCancel(true)  
    .setContentTitle("title")  
    .setContentText("describe")  
    .setContentIntent(pendingIntent)  
    .setSmallIcon(R.drawable.ic_launcher)  
    .setWhen(System.currentTimeMillis())  
    .setOngoing(true);  
notification=builder.getNotification();  

高于API Level 16的版本,就可以用Builder和build()函数来配套的方便使用notification了。

Notification notification = new Notification.Builder(context)    
 .setAutoCancel(true)    
 .setContentTitle("title")    
 .setContentText("describe")    
 .setContentIntent(pendingIntent)    
 .setSmallIcon(R.drawable.ic_launcher)    
 .setWhen(System.currentTimeMillis())    
 .build();   



这就是简单的Notification消息通知。

向AI问一下细节

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

AI