在Android模拟器上按下按钮时发出通知

时间:2011-09-16 09:59:53

标签: java android emulation

当我按下Android模拟器上的a按钮时,如果我想弹出一条消息“Hello”。我应该使用什么代码?再次感谢

1 个答案:

答案 0 :(得分:2)

这是用户点击按钮时通知用户的示例代码

Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnclickListener(){
    @Override
    public void onClick(View v){

      NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      Notification notification = new    Notification(R.drawable.image,"Notification",System.currentTimeMillis());
     Intent notificationIntent = new Intent(Currentclass.this,TargetActivity.class);
     PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
     notification.setLatestEventInfo(getApplicationContext(), "Title", "Sub Title",contentIntent);
     notification.flags=Notification.FLAG_AUTO_CANCEL;
     mNotificationManager.notify(143, notification);

  }

});

此代码可以帮助您