标签:
首先随意扔两个按钮
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="send" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="cencle" /> </LinearLayout>
然后去设置主文件
package com.example.deemo; import android.app.Activity; import android.app.Notification; import android.app.Notification.Builder; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener{ NotificationManager manager;//通知控制类(为了取消) int notification_ID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//获取系统服务(为了取消) findViewById(R.id.button1).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); } @Override public void onClick(View v) {//点击事件 switch (v.getId()) { case R.id.button1: sendNotification(); break; case R.id.button2: manager.cancel(notification_ID);//要取消的id break; } } /** * */ private void sendNotification(){//发送方法 Intent intents = new Intent(this,MainActivity.class); //构造转跳 PendingIntent pintent = PendingIntent.getActivity(this, 0, intents, 0);//即将转跳,非及时 Builder builder = new Notification.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher);//设置图标 builder.setTicker("嘿嘿嘿");//手机状态栏提示 builder.setWhen(System.currentTimeMillis());//设置时间 builder.setContentTitle("我是一个标题"); builder.setContentText("我是一个通知内容"); builder.setContentIntent(pintent);//点击后的意图 builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯 builder.setDefaults(Notification.DEFAULT_SOUND);//设置提示声音 builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动 Notification notification = builder.build();//4.1以上 //4.1以下用builder.getNotification(); manager.notify(notification_ID,notification);//取消,id为对应notification的id } }
比如震动,提示灯要用系统权限,
android.permission.FLASHLIGHT
android.permission.VIBRATE
记得在AndroidManifest设置
标签:
原文地址:http://www.cnblogs.com/webgavin/p/5751159.html