首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击状态栏中的通知消息后通知服务无法工作

单击状态栏中的通知消息后通知服务无法工作
EN

Stack Overflow用户
提问于 2012-04-25 06:28:19
回答 1查看 1.4K关注 0票数 0

我参考了下面的链接来研究Android中通知服务的示例:赛格萨博客Vogella教程

两者都工作,但部分,即我已经下载了这两个项目的现状,并执行他们。两者都有按钮来启动通知。在按钮单击通知出现在顶部状态栏。

问题来了,只要单击通知,就不会显示任何消息,也不会激发任何消息来导航到新的活动。

我对这个概念很陌生,所以任何帮助都要感谢.

编辑

CreateNotification .class

代码语言:javascript
复制
public class CreateNotification extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void createNotification(View view) {
        NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        final int UNIQUE_ID = 123458;
        Intent navigationIntent = new Intent();
        navigationIntent.setClass(CreateNotification.this,
                NotificationReceiver.class);

        PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent,
                0);
        String body = "New Notification added!!!";
        String title = "Title";
        Notification n = new Notification(R.drawable.ic_launcher, body,
                System.currentTimeMillis());
        n.number = 2;
        n.setLatestEventInfo(this, title, body, pi);
        n.defaults = Notification.DEFAULT_ALL;
        nm.notify(UNIQUE_ID, n);
    }
}

NotificationReceiver.class

代码语言:javascript
复制
public class NotificationReceiver extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        Log.i("Receiver", "NotificationReceiver");
    }
}

main.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="createNotification"
        android:text="Create Notification" >
    </Button>

</LinearLayout>

result.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the result activity opened from the notification" >
    </TextView>

</LinearLayout>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-04-25 06:44:33

看看这个

代码语言:javascript
复制
   public static NotificationManager nm;
public static final int UNIQUE_ID = 123458;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newone);
    createNotification();

}
public void createNotification() {
    NotificationManager nm= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    final int UNIQUE_ID = 123458;
    Intent navigationIntent = new Intent();
    navigationIntent.setClass(NEWAct.this,
            TestActivity.class);
    navigationIntent.putExtra("selected_tab", "more");

    PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent,
            0);
    String body = "New Notification added!!!";
    String title = "Title";
    Notification n = new Notification(R.drawable.ic_launcher, body,
            System.currentTimeMillis());
    n.number = 2;
    n.setLatestEventInfo(this, title, body, pi);
    n.defaults = Notification.DEFAULT_ALL;
    nm.notify(UNIQUE_ID, n);
}

活动启动后,必须使用id(UNIQUE_ID)取消通知。

代码语言:javascript
复制
              try {
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.cancel(UNIQUE_ID);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10310425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档