我即将为我的应用程序实现C2DM,但我发现有关如何编写清单的文档有点令人困惑.
清单代码示例包含:
<!-- Only this application can receive the messages and registration result -->
<permission android:name="com.example.myapp.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE" />
Run Code Online (Sandbox Code Playgroud)
这解释如下:
applicationPackage +".permission.C2D_MESSAGE阻止其他应用程序注册和接收应用程序的消息.
但这究竟是如何工作的呢?据我所知,这声明了一个权限,然后获得我的应用程序的权限.但是这个许可究竟在哪里执行?
注册的代码是:
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", emailOfSender);
startService(registrationIntent);
Run Code Online (Sandbox Code Playgroud)
接收registrationIntent的服务如何知道要检查的权限?据我所知(如果我错了,请纠正我),在声明权限时,我可以在我的命名空间中选择任何权限名称,例如com.example.myapp.permission.WHATEVER.
或者C2D_MESSAGE是我必须使用的魔法常数?
此外,文档说我必须实现接收器com.google.android.c2dm.intent.C2D_MESSAGE和com.google.android.c2dm.intent.REGISTRATIONIntents.但在代码示例中,接收者的过滤器仅包含.intent.RECEIVE和.intent.REGISTRATIONIntents.C2D_MESSAGE去哪儿了,它和我上面的问题有什么关系吗?
我希望这不是显而易见的事情,但我只是不明白......请帮忙.
我使用以下代码将密钥转换为字节
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
byte[] bkey=key.getEncoded();
Run Code Online (Sandbox Code Playgroud)
现在如何从中获取密钥bkey?我试过了:
SecretKeySpec secretkey = new SecretKeySpec(bkey,"DES");
SecretKeyFactory sfkey = SecretKeyFactory.getInstance("DES");
SecretKey skey = sfkey.generateSecret(secretkey);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Error during Exception java.security.spec.InvalidKeySpecException: Inappropriate key specification
Run Code Online (Sandbox Code Playgroud) 伙计们我有一个问题,当我们在android中编写一个xml时,我的代码给出了一个Exception作为权限被拒绝.任何人都可以告诉它将如何被删除.
package com.ex.createXml;
import android.app.Activity;
import android.os.Bundle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.xmlpull.v1.XmlSerializer;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;
public class createXml extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File newxmlfile = new File("/data/new.xml");
try{
newxmlfile.createNewFile();
}catch(IOException e)
{
Log.e("IOException", "Exception in create new File(");
}
FileOutputStream fileos = null;
try{
fileos = new FileOutputStream(newxmlfile);
}catch(FileNotFoundException e)
{
Log.e("FileNotFoundException",e.toString());
}
XmlSerializer …Run Code Online (Sandbox Code Playgroud) 我有一个管理第三方服务器列表的应用程序,可以轮询它们以获取信息.这些服务器应该彼此独立(并且可能不在我的控制之下).
现在我想实现c2dm通知以避免持续的主动轮询.第三方服务器将能够发布他们的senderId并允许我的客户在向registrationId谷歌注册后向他们发送C2DM .
现在我的问题是:是否可以从同一个应用程序包中注册多个c2dm发件人?如果是这样,我如何区分注册(尤其是注销)进程,以便我可以更新正确的本地服务器配置条目?
我找到的唯一资源是这个google群组线程,但它暂时没有回答.我希望你能提供帮助.
注意:这只是关于注册.发送消息时,第三方服务器可以注入其服务器名称,因此消息处理程序可以区分它们.但谷歌服务器的注册过程似乎没有给我一个选择将用户定义的信息传递给我的BroadcastReceiver