为什么没有收到来自广播的代码,而是使用adb am?

时间:2019-07-16 13:51:03

标签: android android-intent

我想触发另一个应用程序的接收器。我知道包装名称 操作以及类别和额外数据键名称。

当我从adb shell发布广播时,该广播会被应用接收:

am broadcast -a com.wavelink.intent.action.BARCODE -c android.intent.category.DEFAULT --es com.symbol.datawedge.data_string 09876

从代码中执行类似操作不起作用:

private int sendImplicitBroadcast(Context ctxt, Intent i) {
    int iRet=0;
    PackageManager pm=ctxt.getPackageManager();
    List<ResolveInfo> matches=pm.queryBroadcastReceivers(i, 0);
    if(matches.isEmpty()) {
        addLog("no match found for package name");
    }
    for (ResolveInfo resolveInfo : matches) {
        Intent explicit=new Intent(i);
        ComponentName cn=
                new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName,
                        resolveInfo.activityInfo.name);

        explicit.setComponent(cn);
        addLog("explicit intent for cn="+cn.getPackageName());
        ctxt.sendBroadcast(explicit);
        iRet++;
    }
    return iRet;
}

和意图是:

            Intent intent=new Intent();
                intent.addCategory(txtCategory.getText().toString());

                intent.setAction(txtAction.getText().toString());   // com.wavelink.intent.action.EMDK.SEND or com.wavelink.intent.action.BARCODE

                intent.setClassName(txtPackage.getText().toString(), txtClass.getText().toString());

                intent.setPackage(txtPackage.getText().toString());

            // ##### symbol has extra com.symbol.datawedge.data_string, com.symbol.datawedge.label_type and com.symbol.datawedge.source
            intent.putExtra(getResources().getString(R.string.datawedge_intent_key_data), txtData.getText().toString());
            mysendBroadcast(intent);

然后mysendBroadcast()函数为:

private  void mysendBroadcast(final Intent intent){
    if(sdkVersion<26) {
        sendBroadcast(intent);
        Log.d(TAG, "using sendBroadcast");
    }else {
        Log.d(TAG, "using implict Broadcast");
        //for Android O above "gives W/BroadcastQueue: Background execution not allowed: receiving Intent"
        //either set targetSDKversion to 25 or use implicit broadcast
        if(sendImplicitBroadcast(getApplicationContext(), intent)==0) {
            addLog("Trying normal boradcast...");
            sendBroadcast(intent);
        }
        addLog("sendBroadcastAsForegroundUser...");
        sendBroadcastAsForegroundUser(context, intent);
    }
}

当我调试应用程序时,它甚至找不到包:List match = pm.queryBroadcastReceivers(i,0);返回空列表。

这是怎么了?

0 个答案:

没有答案