public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification, "我是一个通知", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL; //各种消除方式
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:110"));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "我是标题", "我是内容", contentIntent);
nm.notify(0, notification);
}
/**
* 新版本的notification,一般不使用
* @param view
*/
@SuppressLint("NewApi")
public void click2(View view){
Notification noti = new Notification.Builder(this)
.setContentTitle("我是标题")
.setContentText("我是内容")
.setSmallIcon(R.drawable.notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.build();
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(0, noti);
}
}
Notification通知
最新推荐文章于 2022-01-05 22:51:21 发布