1、权限,因为要通过延期意图拨打电话,所以要配置一个可以打电话的权限
<uses-permission android:name="android.permission.CALL_PHONE"/>
2、代码示例
1 package com.example.notifydemo; 2 3 import android.net.Uri; 4 import android.os.Build; 5 import android.os.Bundle; 6 import android.annotation.SuppressLint; 7 import android.annotation.TargetApi; 8 import android.app.Activity; 9 import android.app.Notification; 10 import android.app.Notification.Builder; 11 import android.app.NotificationManager; 12 import android.app.PendingIntent; 13 import android.content.Intent; 14 import android.graphics.BitmapFactory; 15 import android.view.Menu; 16 import android.view.View; 17 18 19 public class MainActivity extends Activity { 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 } 26 27 28 @SuppressLint("NewApi") 29 public void click(View view){ 30 //get a NotificationManager obj 31 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 32 //图标、标题、时间 33 Notification notification = new Notification(R.drawable.ic_launcher,"我是一个notification",System.currentTimeMillis()); 34 notification.flags = Notification.FLAG_AUTO_CANCEL;//这个标志位的设置表示在点击后取消通知栏的该内容 35 /*****意图start*****/ 36 Intent intent = new Intent(); 37 intent.setAction(Intent.ACTION_CALL); 38 intent.setData(Uri.parse("tel:110")); 39 /*****意图end*****/ 40 //第二个参数为请求码,第四个是附加参数 41 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);//延期意图 42 notification.setLatestEventInfo(this, "我是标题", "我是文本", pendingIntent);//延期意图高版本可以为null,低版本的则不能够为null 43 44 //3.0之后才能够使用的代码---start 45 /*Notification.Builder builder = new Builder(this); 46 builder.setContentTitle("我是notification标题") 47 .setContentText("我是notification内容!") 48 .setSmallIcon(R.drawable.ic_launcher) 49 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); 50 Notification notification = builder.build();*/ 51 //3.0之后才能够使用的代码---end 52 53 //第一个参数为id 54 nm.notify(0, notification); 55 } 56 57 }
时间: 2024-10-19 23:06:08