首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从我的通知接收者启动另一个活动

从我的通知接收者启动另一个活动
EN

Stack Overflow用户
提问于 2012-07-10 04:49:26
回答 1查看 2.6K关注 0票数 1

好的,那么,我先说我用的是钛,所以,我想,这会给你带来不好的口感。所以,我很抱歉我对Android方面的知识缺乏了解。

所以,我已经设置了一个接收GCM通知的东西。我可以将它们正确地发送到我的应用程序中,当点击它时,代码会运行并尝试启动我的应用程序。但是,它只是在logcat中给了我以下内容:

代码语言:javascript
复制
I/ActivityManager(  307): START {act=com.geneca.journaling.mobile.GenecaJournalingActivity cat=[android.intent.category.LAUNCHER] flg=0x34000000 bnds=[0,102][720,230] u=0} from pid -1
W/InputMethodManagerService(  307): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@427a60a8 attribute=null

我让C2DM从第三方接收东西,我试图修改它来做我需要的事情,但我显然做了一些不太正确的事情。

下面是onMessage:

代码语言:javascript
复制
@Override
protected void onMessage(Context context, Intent intent) {
    Log.d(LCAT, "Message received");

    HashMap data = new HashMap();
    for (String key : intent.getExtras().keySet()) {
        Log.d(LCAT, "Message key: " + key + " value: "
                + intent.getExtras().getString(key));

        String eventKey = key.startsWith("data.") ? key.substring(5) : key;
        data.put(eventKey, intent.getExtras().getString(key));
    }
    if (C2dmModule.getInstance() == null) {
        int icon = 0x7f020000; // get this from R.java
        CharSequence tickerText = "Geneca Journaling";
        long when = System.currentTimeMillis();

        Bundle extras = intent.getExtras();
        CharSequence contentTitle = "Rate your journal";
        CharSequence contentText = "Notification Content";
        String title = extras.getString("title");
        String message = extras.getString("message");
        if(title != null) {
            contentTitle = title;
        }
        if (message != null) {
            contentText = message;
        }

        //Intent notificationIntent = new Intent(this, C2DMReceiver.class);

        Intent launcherintent = new Intent("com.geneca.journaling.mobile.GenecaJournalingActivity");
        launcherintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.     FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        launcherintent
                .setComponent(ComponentName
                        .unflattenFromString("com.geneca.journaling.mobile.GenecaJournalingActivity"));
        launcherintent.addCategory("android.intent.category.LAUNCHER");


        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                launcherintent, 0);

        // the next two lines initialize the Notification, using the
        // configurations above

        Notification notification = new Notification(icon, tickerText, when);

        notification.defaults = Notification.DEFAULT_ALL;
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        mNotificationManager.notify(1, notification);
    } else
        C2dmModule.getInstance().sendMessage(data);
}

以下是我的清单内容(我想)

代码语言:javascript
复制
<android xmlns:android="http://schemas.android.com/apk/res/android">
    <manifest>
        <activity android:name=".GenecaJournalingActivity"/>
        <permission
            android:name="com.geneca.journaling.mobile.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
        <uses-permission android:name="com.geneca.journaling.mobile.permission.C2D_MESSAGE"/>
        <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
        <uses-permission android:name="android.permission.WAKE_LOCK"/>
        <application>
            <service android:name="com.findlaw.c2dm.C2DMReceiver"/>
            <receiver
                android:name="com.google.android.c2dm.C2DMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                    <category android:name="android.intent.category.HOME"/>
                </intent-filter>
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
                    <category android:name="com.geneca.journaling.mobile"/>
                </intent-filter>
                <intent-filter>
                    <action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
                    <category android:name="com.geneca.journaling.mobile"/>
                </intent-filter>
            </receiver>
        </application>
    </manifest>
</android>

在不同的地方还有另一个清单部分,因为钛是如何处理事情的:

代码语言:javascript
复制
<android:manifest>
    <permission android:name="com.geneca.journaling.mobile.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.geneca.journaling.mobile.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive message -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!-- Send the registration id to the server -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- App must have this permission to use the library -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <activity android:name="com.geneca.journaling.mobile.GenecaJournalingActivity"/>

    <application>
           <!-- In order to use the c2dm library, an
               application must declare a class with the name C2DMReceiver, in its 
               own package, extending com.google.android.c2dm.C2DMBaseReceiver

               It must also include this section in the manifest, replacing 
                "com.google.android.apps.chrometophone" with its package name. 
           -->
        <service android:name="com.findlaw.c2dm.C2DMReceiver" />

        <!-- Only google service can send data messages for the app. If permission is not set -
             any other app can generate it --> 
        <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND">

            <!-- Start receiver on boot -->
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.HOME"/>
            </intent-filter>

            <!-- Receive the actual message -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.geneca.journaling.mobile" />
            </intent-filter>
            <!-- Receive the registration id -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.geneca.journaling.mobile" />
            </intent-filter>
        </receiver>

    </application>
</android:manifest>

如果应用程序已经打开了,它会运行一些钛代码,我想我可以弄清楚,但如果应用程序没有打开,它就会命中onMessage中我不太清楚的东西。

任何帮助都将不胜感激。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-10 04:52:40

这一行:

代码语言:javascript
复制
Intent launcherintent = new Intent("com.geneca.journaling.mobile.GenecaJournalingActivity");

使用操作com.geneca.journaling.mobile.GenecaJournalingActivity创建新的意图。

这是你想要的吗?

代码语言:javascript
复制
Intent intent = new Intent(Intent.ACTION_MAIN);
 intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
  startActivity(intent);

代码语言:javascript
复制
 Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11402884

复制
相关文章

相似问题

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