Android 缺失';额外费用';意图中的数据
我正在将意图从“活动”发送到“服务”中的接收器(并传递数据)。我的代码有活动和服务(有接收者)。接收方声明如下:Android 缺失';额外费用';意图中的数据,android,Android,我正在将意图从“活动”发送到“服务”中的接收器(并传递数据)。我的代码有活动和服务(有接收者)。接收方声明如下: <receiver android:name="xxx" android:enabled="true" > <intent-filter> <action android:name="android.intent.action.PHONE_STATE" /> <
<receiver android:name="xxx"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<!-- protected intents meant for os and not for us <action android:name="android.intent.action.ACTION_NEW_OUTGOING_CALL" android:priority="0" /> -->
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
以上代码成功地从我的应用程序拨打电话。我也有一个接收器来截取通话,接收器截取上述通话很好。然而,上述意图的“额外内容”在接收者中缺失;在下面的代码中,我总是将“电话号码”设为空
Log.e(TAG,"Calling "+number);
Intent callIntent = new Intent(Intent.ACTION_CALL); //ACTION_NEW_OUTGOING_CALL is deprecated in API 21, hence ACTION_CALL
callIntent.putExtra("PHONE_NUMBER",number);
number = "tel:"+number;
callIntent.setData(Uri.parse(number));
startActivity(callIntent);
@Override
public void onReceive(Context context, Intent intent) {
//blah blah..
savedNumber = intent.getExtras().getString("PHONE_NUMBER");
if(savedNumber == null)
savedNumber = intent.getStringExtra("PHONE_NUMBER");
Log.e(TAG, " savedNumber = "+savedNumber);
}
我的错误是什么?为什么我在接受者中得到了意图,但“额外的”却丢失了(正如你可能已经注意到的,我试图从意图中得到两种方式)试试这个:
Intent intent = context.getIntent();
savedNumber = intent.getStringExtra("PHONE_NUMBER");
开始这样的服务-
Intent callIntent=new Intent(this, Service.class);
callIntent.putExtra("phonenumber",number);
this.startService(callIntent);
然后从服务中检索数据
data=(String) intent.getExtras().get("phonenumber");
您可以从onHandleIntent或onStartCommand意图参数访问参数
服务
protected void onStartCommand (Intent intent, int flags, int startId) {
data=(String) intent.getExtras().get("data");
}
意向服务
protected void onHandleIntent(Intent intent) {
data=(String) intent.getExtras().get("data");
}
这取决于您正在运行的服务类型。getIntent()是活动类的方法。您可以看到,在onReceive()方法中有一个intent参数,您可以从中获取字符串
String number = null;
number = intent.getStringExtra("PHONE_NUMBER");
但我在这篇文章上读到:
只有安卓系统本身才能广播新的呼叫意图。
你不能将你自己的额外内容添加到这个意图中。你需要想出另一种方法来完成你想要完成的任何事情。我不能调用“this.getIntent”,因为代码在接收者的“onReceive”方法中,而不是在活动中(编辑了我帖子中截取的最后一个代码)@statosdotcom-而不是“this”context@kashili,使用上下文而不是“这个“这就是我开始给卡斯希里写的东西@cafebabe1991,谢谢,我没有写上下文,因为问题在实际版本之前没有显示这一点。我也会编辑我的答案。是的,我理解。新的呼出呼叫是受保护的意图。因此,正如您在我的xml代码片段中所看到的那样,我不使用NEW_OUTGOING_CALL。哦,非常抱歉,ACTION_CALL也是一个受保护的操作:)它允许您直接拨打电话号码,而无需用户交互。这是一种安全风险,因此它受到许可的“保护”。您还将注意到Manifest.permission并没有公开定义该权限:这是系统中故意的。不,我不能像您建议的那样启动服务。因为按钮的操作处理程序调用调用意图(多次)。您假设我在开始服务之前知道“额外服务”的号码