1

这就是我在完成第一个活动的任务后调用的通知类中的代码。

但是我在获取有关当前应用程序的通知时遇到了问题。

我想将通知显示为对话框。

"R.layout.main" 

包含带有确定按钮的对话框。

public class Notif extends Activity implements View.OnClickListener {

  private Button Button01;

  private NotificationManager mManager;

  private static final int APP_ID = 0; 

  @Override

  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    this.Button01 = (Button) this.findViewById( R.id.Button1);

    this.Button01.setOnClickListener(this);



    mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

  }

  @Override

  public void onClick(View v) {

    Intent intent = new Intent(this,Notif.class);
    Notification notification = new Notification(R.drawable.icon,

    "Notify", System.currentTimeMillis());

       notification.setLatestEventInfo(Notif.this,"App Name","Description of the                     notification",PendingIntent.getActivity(this.getBaseContext(), 0, intent,

PendingIntent.FLAG_CANCEL_CURRENT));

    mManager.notify(APP_ID, notification);

  }

}
4

1 回答 1

2

1) 为什么要使用 View.OnClickListener 的实现来处理您的按钮侦听器?

到目前为止我看到的标准方式是:

    Button01.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Your code
        }
    });

2) 通知是通过屏幕顶部的 Android 状态面板通知用户的方式。我不明白你想用通知和对话框做什么 - 决定你想要哪个?

http://developer.android.com/guide/topics/ui/dialogs.html

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

如果您确实想使用通知,那么这就是我在我的onStop()方法中所拥有的(基本上就是您从遵循 Android 指南中得到的):

        Notification notification = new Notification(R.drawable.icon, "App Name", System.currentTimeMillis());
        notification.flags = Notification.FLAG_AUTO_CANCEL;

        Intent notificationIntent = new Intent(this, ClassToStart.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(getApplicationContext(), "App Name", "Press here to resume", contentIntent);
        mNotificationManager.notify(1, notification);

这是mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);已经完成的onCreate()

真的不确定您要做什么。

于 2011-03-17T11:17:52.097 回答