如何当用户点击一个通知开始的活动? [英] How to start activity when user clicks a notification?

查看:156
本文介绍了如何当用户点击一个通知开始的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一些code我发现了一个教程为我所用。本来,code启动系统联系人列表时,用户可以点击我的应用程序生成的通知。我试图启动活动我自己的,而不是发动联系人列表,但它不工作。更具体地讲,没有任何反应。没有错误,我的活动不加载任何。点击后,通知窗口消失,原活动仍是可见的。

下面是我的code:

 公共类MyBroadcastReceiver扩展的BroadcastReceiver {
    私人NotificationManager mNotificationManager;
    私人诠释SIMPLE_NOTFICATION_ID;

    公共无效的onReceive(上下文的背景下,意图意图){
        捆绑额外= intent.getExtras();

        字符串处理=(字符串)extras.get(交易);
        字符串标题=新政发现,在+(字符串)extras.get(LOCATIONNAME);

        mNotificationManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        通知notifyDetails =新的通知(R.drawable.icon,标题,System.currentTimeMillis的());

        类ourClass;
        尝试 {
            ourClass =的Class.forName(com.kjdv.gpsVegas.ViewTarget);
            意图startMyActivity =新的意图(背景下,ourClass);

            PendingIntent myIntent = PendingIntent.getActivity(上下文,0,startMyActivity,0);
            notifyDetails.setLatestEventInfo(背景下,产权,交易,myIntent);
            notifyDetails.flags | = Notification.FLAG_AUTO_CANCEL;
            notifyDetails.flags | = Notification.DEFAULT_SOUND;
            mNotificationManager.notify(SIMPLE_NOTFICATION_ID,notifyDetails);
        }
        赶上(ClassNotFoundException异常E){
            e.printStackTrace();
        }
    }
}
 

这是我在 AndroidManifext.xml 文件条目...

 <活动机器人:ViewTarget名称=机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=com.kjdv.gpsVegas.ViewTarget/>
            <类机器人:名称=android.intent.category.DEFAULT/>
        &所述; /意图滤光器>
   < /活性GT;
 

这是我的活动,我想推出...

 公共类ViewTarget扩展ListActivity {
    公共ListAdapter getListAdapter(){
        返回super.getListAdapter();
    }

    公众的ListView getListView(){
        返回super.getListView();
    }

    公共无效setListAdapter(ListAdapter适配器){
        super.setListAdapter(适配器);
    }

    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.locations);
        Log.v(DB,内部ViewTarget);
    }
}
 

解决方案

我想通了这个问题。我忘了,包括在清单文件中的活性声明包的名称。

错误:

 活动机器人:名称=机器人ViewTarget。:标签=@字符串/ APP_NAME
 

正确的:

 活动的android:NAME =com.kjdv.gpsVegas.ViewTarget机器人:标签=@字符串/ APP_NAME
 

I am attempting to convert some code I found in a tutorial for my own use. Originally, the code launched the system contacts list when the user would click a notification generated by my app. I am trying to start an Activity of my own instead of launching the contact list, but it's not working. More specifically, nothing happens. There is no error, and my Activity doesn't load either. The notification window disappears after clicking, and the original Activity is still visible.

Here is my code:

public class MyBroadcastReceiver extends BroadcastReceiver {
    private NotificationManager mNotificationManager;
    private int SIMPLE_NOTFICATION_ID;

    public void onReceive(Context context, Intent intent){
        Bundle extras = intent.getExtras();

        String deal = (String) extras.get("Deal");
        String title = "Deal found at " + (String) extras.get("LocationName");

        mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notifyDetails = new Notification(R.drawable.icon, title,System.currentTimeMillis());

        Class ourClass;
        try {
            ourClass = Class.forName("com.kjdv.gpsVegas.ViewTarget");
            Intent startMyActivity = new Intent(context, ourClass);

            PendingIntent myIntent = PendingIntent.getActivity(context, 0,startMyActivity, 0);
            notifyDetails.setLatestEventInfo(context, title, deal, myIntent);
            notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
            notifyDetails.flags |= Notification.DEFAULT_SOUND;
            mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

This is my entry in the AndroidManifext.xml file...

  <activity android:name=".ViewTarget" android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.kjdv.gpsVegas.ViewTarget" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
   </activity>

And this is my Activity that I want to launch...

public class ViewTarget extends ListActivity {
    public ListAdapter getListAdapter() {
        return super.getListAdapter();
    }

    public ListView getListView() {
        return super.getListView();
    }

    public void setListAdapter(ListAdapter adapter) {
        super.setListAdapter(adapter);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.locations);
        Log.v("db", "Inside ViewTarget");
    }
}

解决方案

I figured out the problem. I forgot to include the package name in the activity declaration in the Manifest file.

Wrong:

activity android:name=".ViewTarget" android:label="@string/app_name" 

Correct:

activity android:name="com.kjdv.gpsVegas.ViewTarget" android:label="@string/app_name" 

这篇关于如何当用户点击一个通知开始的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆