点击手机状态栏Notification,弹出 AlertDialog的方法

一、实现的思路

  点击Notification,跳转到一个空白的activity,在空白的activity种弹出AlertDialog ,然后finish()。

  Notification的使用方法,1.通过系统服务拿到NotificationManager。2.使用NotifiactionManager的notify方法

二、代码如下

 

public class NotificationTest extends Activity
{
static final int NOTIFICATION_ID = 0x1123;
@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);


  //获取应用界面中的Button对象
  Button bn = (Button) findViewById(R.id.bn);
  //为按钮的单击事件绑定事件监听器
  bn.setOnClickListener(new View.OnClickListener()
  {
    @Override
  public void onClick(View source)
  {
    //创建一个启动其他Activity的Intent
    Intent intent = new Intent(NotificationTest.this, OtherActivity.class);
    PendingIntent pi = PendingIntent.getActivity(NotificationTest.this , 0, intent , 0);
    //创建一个Notification
    Notification notify = new Notification();
    //为Notification设置图标,该图标显示在状态栏
    notify.icon = R.drawable.notify;
    //为Notification设置文本内容,该文本会显示在状态栏
    notify.tickerText = "启动其他Activity的通知";
  
    notificationManager.notify(NOTIFICATION_ID, notify);
  }
});

  }
}

 

public class OtherActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // 设置该Activity显示的页面
  setContentView(R.layout.other);
  NotificationManager notificationManager = (NotificationManager)
  getSystemService(NOTIFICATION_SERVICE);
  //取消通知
  notificationManager.cancel(NotificationTest.NOTIFICATION_ID);

  new AlertDialog.Builder(OtherActivity.this).setTitle("选择文件")
  .setMessage("确认选择该文件?")
  .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub

     //获取系统的NotificationManager服务
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //取消通知
    notificationManager.cancel(NOTIFICATION_ID);

        finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
           Toast.makeText(OtherActivity.this, "取消选择",Toast.LENGTH_SHORT).show();

                finish();
         }
     }).show();
}

posted on 2014-03-25 17:58  axiaoquana  阅读(461)  评论(0)    收藏  举报

导航