Android通知的实现

来源:互联网 发布:淘宝店信誉级别 编辑:程序博客网 时间:2024/05/21 21:38

在安卓开发中 通常会用到通知功能  这一功能主要使用的是android.app.NotificationManager;这一个包中的Notification功能。

NotificationManager notificationManager=(NotificationManager)
        getSystemService(NOTIFICATION_SERVICE);调用系统的service。

public class MainActivity extends Activity {    protected static final int NOTIFYID_1 = 0;    final  int CODE=0x17;@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final NotificationManager notificationManager=(NotificationManager)        getSystemService(NOTIFICATION_SERVICE);               Button button1=(Button)this.findViewById(R.id.button1);       final Button button3=(Button)this.findViewById(R.id.button3);        button1.setOnClickListener(new OnClickListener() {@SuppressWarnings("deprecation")public void onClick(View v) {Notification notification=new Notification();notification.icon=R.drawable.ic_launcher;notification.tickerText="这是一个通知";notification.when=System.currentTimeMillis();  //设置发送时间notification.defaults=Notification.DEFAULT_ALL;  //设置默认的声音和震动以及闪光灯notification.setLatestEventInfo(MainActivity.this, "Hello", "限时特卖了", null);notificationManager.notify(NOTIFYID_1,notification);notification.defaults|=Notification.FLAG_AUTO_CANCEL;Intent it=new Intent(getBaseContext(), MainActivity.class);    PendingIntent pendintent=PendingIntent.getActivity(getBaseContext(), 0, it, 0);    notificationManager.notify(0, notification);}});                button3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//notificationManager.cancel(NOTIFYID_1);String user=((EditText)findViewById(R.id.editText1)).getText().toString();Intent intent=new Intent(MainActivity.this,NextActivity.class);Bundle bundle=new Bundle();bundle.putCharSequence("user", user);intent.putExtras( bundle);startActivityForResult(intent, CODE);}});    }protected void onActivityResult(int requestCode,int resultCode,Intent data){super.onActivityResult(requestCode, resultCode, data);if (requestCode==CODE&&resultCode==CODE) {((EditText)findViewById(R.id.editText1)).setText("");((EditText)findViewById(R.id.editText1)).setHint("123");}}    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    }
Notification notification=new Notification();   //实例化notification
notification.icon=R.drawable.ic_launcher;notification.tickerText="这是一个通知";notification.when=System.currentTimeMillis();  //设置发送时间notification.defaults=Notification.DEFAULT_ALL;  //设置默认的声音和震动以及闪光灯notification.setLatestEventInfo(MainActivity.this, "Hello", "限时特卖了", null);notificationManager.notify(NOTIFYID_1,notification);notification.defaults|=Notification.FLAG_AUTO_CANCEL;
当然在调用闪光灯和震动功能的时候,需要在manifest中添加权限:

     <uses-permission android:name="android.permission.FLASHLIGHT"/>    <uses-permission android:name="android.permission.VIBRATE" />





0 0