【问题标题】:How to hide notification on click and show an activity in android如何隐藏点击通知并在android中显示活动
【发布时间】:2011-12-16 00:08:00
【问题描述】:

我正在开发一个应用程序来启动一个服务来检查通知。

public class ServiceNotification extends Service{
private ThreadNotification notifica;
private NotificationManager mManager;
private String id;
@Override
public void onCreate() {
    super.onCreate();
    SharedPreferences settings = getSharedPreferences("setting", 0);
    String id = settings.getString("id", "no");
    this.id=id;
    mManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    //notifica = new ThreadNotification(id,getApplicationContext(),getBaseContext());
}
private void showNotification(int id, String client, String notifica) {
    Notification notification = new Notification(R.drawable.icon, "Notifica da "+client, 10000);
    CharSequence contentTitle = "Notifica da "+client;
    CharSequence contentText = notifica;
    Intent notificationIntent = new Intent();
    notificationIntent.setClassName("package", "package.ActivityNotification");
    notificationIntent.putExtra("title",title);
    notificationIntent.putExtra("notification",notification);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent,Notification.FLAG_AUTO_CANCEL);
    notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
    mManager.notify(id, notification);
}
@Override
public void onStart(Intent intent, int startId) { 
    Thread t = new Thread(new Runnable(){
        public void run(){
            int i =0;
            while(true){
                String page = "http://mysite.com/notification.php?"+id;
                String risposta = "";
                try {
                    URL url = new URL(page);
                    URLConnection connection = url.openConnection();
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                                    connection.getInputStream()));
                    risposta = in.readLine();
                    in.close();
                        String[] all = risposta.split("@@@");
                        String notification = all[0];
                        String title = all[1];
                        showNotification(i++, title, notification);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    try {
                        Thread.sleep(20000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }
    });
    t.start();
}
@Override
public void onDestroy() {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

这是点击通知时我想要加载的活动

public class ActivityNotification extends Activity{
private String title,notification;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //this.c=this;
    title = getIntent().getStringExtra("title");
    notification = getIntent().getStringExtra("notification");
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Notifica da "+title);
    alertDialog.setMessage(notification);
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.cancel();
        }
    });
    alertDialog.setIcon(R.drawable.errore_icon);
    alertDialog.show();
}

}

显示通知,但是当我单击通知时,什么也没有发生。通知仍然存在,并且未加载活动。我能做些什么?你能帮我吗??

编辑:使用

notification.flags |=Notification.FLAG_AUTO_CANCEL;

点击时通知将被隐藏,但现在仍然显示活动!

【问题讨论】:

  • 你试过notificationIntent.setClassName("packageName", "ActivityNotification");吗?
  • 是的,看起来你实际上并没有通过一个类来启动你的意图。只需添加它,一切都应该正常。
  • 我这样尝试 Intent notificationIntent = new Intent(this,ActivityNotification.class); ,换一种方式,notificationIntent.setClassName("packageName", "ActivityNotification");但什么都没有:(

标签: android android-activity notifications android-service


【解决方案1】:

解决了!这都是我的错。我忘记将我的活动添加到我的清单中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多