Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在用户单击通知时启动活动?_Java_Android_Notifications_Android Activity - Fatal编程技术网

Java 如何在用户单击通知时启动活动?

Java 如何在用户单击通知时启动活动?,java,android,notifications,android-activity,Java,Android,Notifications,Android Activity,我正试图将在教程中找到的一些代码转换为我自己使用的代码。最初,当用户单击我的应用程序生成的通知时,代码启动了系统联系人列表。我正在尝试启动自己的活动,而不是启动联系人列表,但它不起作用。更具体地说,什么也没有发生。没有错误,我的活动也不会加载。单击后通知窗口消失,原始的活动仍然可见 这是我的密码: public class MyBroadcastReceiver extends BroadcastReceiver { private NotificationManager mNotifi

我正试图将在教程中找到的一些代码转换为我自己使用的代码。最初,当用户单击我的应用程序生成的通知时,代码启动了系统联系人列表。我正在尝试启动自己的
活动
,而不是启动联系人列表,但它不起作用。更具体地说,什么也没有发生。没有错误,我的
活动
也不会加载。单击后通知窗口消失,原始的
活动仍然可见

这是我的密码:

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();
        }
    }
}
这是我在
AndroidManifext.xml
文件中的条目

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

您是否可以尝试删除意图过滤器,使其看起来像这样:

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

此外,不确定此代码是否有效:

ourClass=Class.forName(“com.kjdv.gpsVegas.ViewTarget”);
意向startMyActivity=新意向(上下文,我们的类)

你能这样试一下吗


Intent startMyActivity=新的Intent(上下文,ViewTarget.class)

您运行的是哪个Android版本?您可能想尝试使用NotificationCompat。此类包含在最新的支持包中

Intent notificationIntent=新意图(上下文,ViewTarget.class);
PendingEvent contentIntent=PendingEvent.getActivity(上下文,
0,通知意图,
挂起内容标志(取消当前);
NotificationManager nm=(NotificationManager)上下文
.getSystemService(上下文通知服务);
Resources res=context.getResources();
NotificationCompat.Builder=新建NotificationCompat.Builder(上下文);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.app_图标)
.setLargeIcon(位图工厂.decodeResource(res,R.drawable.app_图标))
.setTicker(有效载荷)
.setWhen(System.currentTimeMillis())
.setAutoCancel(真)
.setContentTitle(“消息”)
.setContentText(有效载荷);
通知n=builder.getNotification();
n、 默认值|=Notification.DEFAULT_ALL;
nm.notify(0,n);
编辑: 我知道这是一个旧的线程/问题,但这个答案帮助我在点击通知时显示活动。 对于那些不起作用的人来说,这可能是因为您没有在清单中“注册”活动。例如:


希望这能奏效。 希望它有帮助…

检查此代码

            public class TestActivity extends Activity {
private static final int UNIQUE_ID = 882;

public static NotificationManager nm;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Intent navigationIntent = new Intent();


    navigationIntent.setClass(classname.this, MainActivity.class);


    PendingIntent pi = PendingIntent.getActivity(this, 0, navigationIntent,
            0);

    String body = "New notificattion added!!!";
    String title = "Notification";


    Notification n = new Notification(R.drawable.icon, body,
            System.currentTimeMillis());

            //this is for giving number on the notification icon

    n.number = Integer.parseInt(responseText);

    n.setLatestEventInfo(this, title, body, pi);
    n.defaults = Notification.DEFAULT_ALL;

    nm.notify(UNIQUE_ID, n);

为了从
意图
启动
活动
,您需要添加一个标志:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

即使在
Intent
的构造函数中声明类,这也是正确的。

我解决了问题。我忘了在清单文件的活动声明中包含包名

错:

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

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

您应该为意图设置操作和类别

Intent startMyActivity = new Intent(context, ourClass);
startMyActivity .setAction(Intent.ACTION_MAIN);
startMyActivity .addCategory(Intent.CATEGORY_LAUNCHER);

它起作用了

2.2我将对此进行研究。谢谢我在2.1+上测试了我的,所以这应该可以用。现有通知已被弃用(可能不会很快)。我是否做错了什么?我粘贴了代码,Eclipse无法解析NotificationCompat,也没有提供导入建议。这是从哪个名称空间来的?ThanksI添加了代码。但它不会加载活动。通知将激发,单击时不会发生任何事情。通知消失,默认屏幕仍然可见。有什么想法吗?奇怪,我建议您执行以下操作之一:1)删除意图过滤器,并确保您的ViewTarget类与指定的类相同。2) 添加一个新活动以检查它是否启动(可能是hello world)。我开始认为ViewTarget活动中的某些东西在它有机会做出反应之前就杀死了它。只是一个猜测,但值得一试。嗯……这不管用。我将nm.notify改为n.notify(假设是打字错误)。然后Eclipse从Notify()方法调用中删除了我的参数以匹配API。是否有一个版本的Notification.Nofity有参数?很抱歉,回复太晚…我正在编辑我的代码。nm是Notification Manager的实例。我添加了这个。不幸的是,这没有什么区别。活动未加载。只是坐在默认/主活动屏幕上,删除了意图过滤器,并将我的意图初始化代码替换为你的。仍然没有:(只是在单击通知后坐在默认/主活动处。ViewTarget活动是否已在运行(这是您的默认/主活动吗?)不,它没有运行。有一个单独的活动是主要活动。我的一个应用程序中有类似的代码…现在唯一真正的区别是你在调用
notify
时设置的ID。你能试试
(int)System.currentTimeMillis()吗
而不是
SIMPLE\u NOTIFICATION\u ID
?如果ID为0,则调用notify也可能不起作用。(可能是0,因为您从未初始化SIMPLE\u NOTIFICATION\u ID)我解决了问题。我在清单文件中的活动声明中省略了包名。我改为。现在一切正常。谢谢!
Intent startMyActivity = new Intent(context, ourClass);
startMyActivity .setAction(Intent.ACTION_MAIN);
startMyActivity .addCategory(Intent.CATEGORY_LAUNCHER);