Android C2DM 和 lib 项目

发布于 2025-01-07 18:55:16 字数 1400 浏览 1 评论 0原文

我目前正在开发一个 libproject (Android),它应该包含在其他一些应用程序中。

现在一切都工作正常,除了 C2DM 之外,我在活动和清单方面遇到了一些困难。

我可以很好地调用我的不同类,但我似乎无法捕获注册ID(或者当然是实际消息,但这一定是同一个问题......)

我认为问题来自于我的清单中的过滤( s),所以如果有人对我有任何建议,那将非常有帮助。

这是我的清单的接收器部分的副本(来自应用程序,而不是库,但它实际上只是一个副本),但它非常简单。我只是想知道应该如何调整它才能调用 lib 中正确的类...

        <!--
             Only C2DM servers can send 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" >

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

其中 com.myapp.lib 是我的 lib 包名称,接收器位于名称相同的包中(在 lib 项目中) , 当然)。

预先感谢您的帮助,请随时询问更多详细信息:)

编辑: 我尝试仅使用在 google C2DM 上注册的库,也尝试使用应用程序和库。同样的问题

I'm currently working on a libproject (Android) that should be included inside a few other applications.

Everything is working fine now that I've been struggling a bit with Activities and Manifests, exept for the C2DM bit.

I can invoke my different classes fine, but I can't seem to catch the registration ID (or of course actual messages, but that must be the same problem...)

I think the problem is coming from the filtering in my manifest(s), so if anyone has any advice for me, that would be really helpful.

Here is a copy of receiver part of my manifest (from the apps, not the library, but it's actually just a copy), but it's pretty straighforward. I just want to know how I should adapt it in order to invoke the right class in the lib...

        <!--
             Only C2DM servers can send 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" >

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

Where com.myapp.lib is my lib package name, and the receiver is in a package named the same (in the lib project, of course).

Thanks in advance for the help, and don't hesitate to ask for furthers details :)

Edit :
I tried with only the lib registered on google C2DM, and also with both app and lib. Same problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

死开点丶别碍眼 2025-01-14 18:55:16

有一种更好的方法可以通过使用意图过滤器从库项目中使用 C2DM。

清单文件是来自应用程序的文件。
lib 的包是 com.mylib,应用程序的包是 com.myapp。

lib 清单中有两处需要更改。

  1. 权限中使用的类路径。
  2. 意图过滤器包。

两者都应该是您的应用程序包,而不是您的 lib 包。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp"
android:versionCode="1"
android:versionName="1.0" >

<permission android:name="com.myapp.permission.C2D_MESSAGE" android:protectionLevel="signature" />

<uses-permission android:name="com.myapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

     <uses-library android:name="com.google.android.maps" android:required="true"/>



    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


     <receiver
        android:name="com.mylib.C2DMRegistrationReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" >
            </action>
            <category android:name="com.myapp" /> 
        </intent-filter>
    </receiver>

    <receiver
        android:name="com.mylib.C2DMMessageReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" >
            </action>
           <category android:name="com.myapp" /> 
        </intent-filter>
    </receiver>
</application>
</manifest>

There is a better way of using your C2DM from a library project by using the intent-filter.

The manifest file is the one from the app.
The package for the lib is com.mylib and the one for the app is com.myapp.

There is 2 things to change from the lib manifest.

  1. The classpath used in permissions.
  2. The intent-filter package.

Both should be you app package and not your lib package.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp"
android:versionCode="1"
android:versionName="1.0" >

<permission android:name="com.myapp.permission.C2D_MESSAGE" android:protectionLevel="signature" />

<uses-permission android:name="com.myapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name" >

     <uses-library android:name="com.google.android.maps" android:required="true"/>



    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


     <receiver
        android:name="com.mylib.C2DMRegistrationReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" >
            </action>
            <category android:name="com.myapp" /> 
        </intent-filter>
    </receiver>

    <receiver
        android:name="com.mylib.C2DMMessageReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" >
            </action>
           <category android:name="com.myapp" /> 
        </intent-filter>
    </receiver>
</application>
</manifest>
灼疼热情 2025-01-14 18:55:16

答案,是否有人偶然发现同样的问题...

在 google.android.c2dm 包中,类 C2DMBaseReceiver,方法 runIntentInService,

String receiver = context.getPackageName() + ".C2DMReceiver"

使用完全限定名称进行更改..然后就可以了:)

Answer, is anyone stumble upon the same problem...

In the google.android.c2dm package, class C2DMBaseReceiver, method runIntentInService, change

String receiver = context.getPackageName() + ".C2DMReceiver"

with the fully qualified name.. and there you go :)

吹泡泡o 2025-01-14 18:55:16

清单看起来不错。在您的包中,您应该有一个名为 C2DMReceiver 的类,并且它应该扩展 C2DMBaseReceiver。然后,在成功注册和收到消息时调用该类及其包含的重写方法。我写了一个非常基本的示例,参考此处可能会对您有所帮助

The manifest looks fine. In your package you should have a class called C2DMReceiver and it should extend C2DMBaseReceiver. This class and the overridden methods it contains are then called upon successful registration and when a message is received. I have written a very basic example of this that may be helpful for you to refer to here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文