Android 里的各类通讯方式(三)

四:notifation 通知app

  Notifation通知栏会在屏幕上方向用户提示信息 可是不会打断用户正在阅读的内容,除非用户手动将 Notifation通知栏拉下。 Notifation的好处就是在于不会影响用户的操做,好比用户正在阅读很是重要的信息这时候帮他直接打开一个activity会很是不合适 由于直接影响到了他当时的操做行为 因此Notifation就出来了。建议你们在开发中遇到可能打断用户使用的状况下都去使用Notifation通知栏。ide

public class NotificationActivity extends Activity {    this

    NotificationManager mManager = null;    code

    Notification notification =null;    对象

    @Override    开发

    protected void onCreate(Bundle savedInstanceState) {    字符串

    setContentView(R.layout.notification);    get

    // 获得通知消息的管理器对象,负责管理 Notification 的发送与清除消息等    string

    mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);it

    // 建立Notification对象 参数分别表明 通知栏 中显示的图标 显示的标题 显示的时间    

    notification = new Notification(R.drawable.jay,    

    "Android专业开发群", System.currentTimeMillis());    

    // 设置在通知栏中点击后Notification自动消失    

    notification.flags = Notification.FLAG_AUTO_CANCEL;    

    //设置点击后转跳的新activity    

    Intent intent = new Intent(this, MyShowActivity.class);    

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);    

    //经过bundle能够带一些数据过去 这里将字符串传递了过去    

    Bundle bundle = new Bundle();    

    bundle.putString("name", "从Notification转跳过来的");    

    intent.putExtras(bundle);       

    //设置通知栏中显示的内容    

    PendingIntent contentIntent = PendingIntent.getActivity(this,    

        R.string.app_name, intent, PendingIntent.FLAG_UPDATE_CURRENT);    

    notification.setLatestEventInfo(this, "Android专业开发群",    

     "QQ群号 164257885", contentIntent);    

    Button button0 = (Button)findViewById(R.id.button0);    

    button0.setOnClickListener(new OnClickListener() {        

        @Override    

        public void onClick(View arg0) {    

        //打开这个Notification通知    

        mManager.notify(0, notification);    

        }    

    });     

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

    button1.setOnClickListener(new OnClickListener() {      

        @Override    

        public void onClick(View arg0) {    

        //关闭这个Notification通知    

        mManager.cancelAll();    

        }    

    });      

    super.onCreate(savedInstanceState);    

    }   

}    

相关文章
相关标签/搜索