2011-03-17 115 views
1

所有我这是我的通知类中的代码,我从第一次活动完成我的任务后调用。Android中的通知问题?

但我遇到了当前应用程序获取通知的问题。

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

"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); 

    } 

} 

回答

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()

真的不知道你在做什么。

+0

感谢您对我的时间...实际上,我已创建活动,其中我有进度条,其中有一个最大限制..所以在达到进度条到最大值我想显示消息box.it必须看到任何当前的应用程序..因此,这是你想创建对话框作为通知..请帮助我实现它... – Smith 2011-03-17 11:55:59

+0

你想创建一个通知和一个弹出框?我真的认为你应该坚持创建一个通知,他们很难错过,并应该很好地通知用户(这是他们的目的)。然后你可以在你的活动中弹出一个对话框,如果你需要从那里显示更多的信息。我不认为你应该尝试从一个不重点的活动中做通知和对话框(理想情况下,你应该做一个服务的通知,我想,但这是另一回事)。 – Klaus 2011-03-17 12:08:18

+0

@你的权利,但我可以在屏幕中间显示消息窗口作为通知,我想把按钮放在该消息框.....请帮助实现它 – Smith 2011-03-17 12:19:42