我正在寻找通过处理未找到外部包的可能性来通知用户他们需要安装包的最佳方法。
在这种特殊情况下,我希望在C2DMMessaging类中实现TODO: if intent not found, notification on need to have GSF
public static void register(Context context,
String senderId) {
Intent registrationIntent = new Intent(REQUEST_REGISTRATION_INTENT);
registrationIntent.setPackage(GSF_PACKAGE);
registrationIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
PendingIntent.getBroadcast(context, 0, new Intent(), 0));
registrationIntent.putExtra(EXTRA_SENDER, senderId);
context.startService(registrationIntent);
// TODO: if intent not found, notification on need to have GSF
}
我在想我应该查找错误W/ActivityManager( 60): Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gsf (has extras) }: not found
但如何捕获该错误?
UPDATE - 刚发现对startService的调用返回一个ComponentName实例,如果服务启动失败,该实例为null,所以我的代码现在看起来像这样
ComponentName name = context.startService(registrationIntent);
// TODO: if intent not found, notification on need to have GSF
if(name == null){
Util.log_debug_message("@@@@ REG INTENT FAILED");
}else{
Util.log_debug_message("@@@@ REG INTENT SUCCEEDED");
}
(对于任何寻找此解决方案的人来说,Util.log_debug只是我在util类中创建的一个函数来调用Log.d所以只需用对Log.d的调用来替换它。)
这似乎工作正常所以我想我需要发送一个额外的广播消息,以指示需要安装包。然后,接收器可以显示一个警告对话框,说明用户必须安装!
用户需要安装什么?以及用户如何安装需要安装的东西?
提前感谢任何提示,指针代码段和帮助
答案 0 :(得分:2)
我已经解决了这个问题 - 在调用C2DM注册事件的活动中,我有这个代码
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
register();
}
protected void register() {
String reg_id = C2DMessaging.getRegistrationId(this);
String email = Util.getEmail(this);
if(reg_id == null || reg_id == ""){
Util.log_debug_message("@@@@ Registering with C2DM");
Toast.makeText(this, "Registering with C2DM", Toast.LENGTH_LONG).show();
if(C2DMessaging.register(this, Config.C2DM_SENDER)){
showLoadingDialog();
}else{
showInstallGSFDialog();
}
}else if(email == null || email =="-1"){
Util.log_debug_message("**** Updating server with new auth token");
register_with_server();
}
}
private void showInstallGSFDialog(){
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("ERROR!");
alertDialog.setMessage("Please ensure you have a valid GMail account set up on your phone." +
" This application needs to use Google's C2DM service");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
alertDialog.show();
}
我将C2DMessaging.register更改为布尔方法并添加了检查以确保服务启动如此......
/**
* Initiate c2d messaging registration for the current application
*/
public static boolean register(Context context, String senderId) {
boolean res = false;
Intent registrationIntent = new Intent(REQUEST_REGISTRATION_INTENT);
registrationIntent.setPackage(GSF_PACKAGE);
registrationIntent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
PendingIntent.getBroadcast(context, 0, new Intent(), 0));
registrationIntent.putExtra(EXTRA_SENDER, senderId);
ComponentName name = context.startService(registrationIntent);
// if intent not found, notification on need to have GSF by NOT setting resukt of this function to true
if(name == null){
Util.log_debug_message("@@@@ REG INTENT FAILED");
}else{
Util.log_debug_message("@@@@ REG INTENT SUCCEEDED");
res = true;
}
return res;
}