Android 如何将值从活动传递给广播接收者?
我正在使用broadcastreceiver开发一个应用程序。我对活动中广播接收器的接收值有问题,请告诉我如何将活动中的值发送到广播接收器,以及如何从活动中接收广播接收器中的值,我正在尝试以下代码,但它不起作用 我的代码 在活动方面Android 如何将值从活动传递给广播接收者?,android,android-broadcast,Android,Android Broadcast,我正在使用broadcastreceiver开发一个应用程序。我对活动中广播接收器的接收值有问题,请告诉我如何将活动中的值发送到广播接收器,以及如何从活动中接收广播接收器中的值,我正在尝试以下代码,但它不起作用 我的代码 在活动方面 Intent intent = new Intent("my.action.string"); //intent.setAction("IncomCallBroadCast"); intent.putExtra("contact",phNo);
Intent intent = new Intent("my.action.string");
//intent.setAction("IncomCallBroadCast");
intent.putExtra("contact",phNo);
sendBroadcast(intent);
在接收端
String action = intent.getAction();
Log.i("Receiver", "Broadcast received: " + action);
if(action.equals("my.action.string")){
contact = intent.getExtras().getString("contact");
Log.e("",contact );
}
manifest
<action android:name="my.action.string"/>
String action=intent.getAction();
Log.i(“接收器”,“接收到的广播:”+动作);
if(action.equals(“my.action.string”)){
contact=intent.getExtras().getString(“contact”);
Log.e(“”,联系人);
}
显示
在主活动中,使用此代码启动广播接收器
Intent intent = new Intent("package.action.string");
intent.putExtra("extra", phoneNo); \\ phoneNo is the sent Number
sendBroadcast(intent);
在广播接收器中,请使用此代码
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Receiver", "Broadcast received: " + action);
if(action.equals("package.action.string")){
String state = intent.getExtras().getString("extra");
}
}
注意:不要忘记在清单文件中声明您的广播接收器
<receiver android:name=".SmsReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="package.action.string" />
<!-- and some more actions if you want -->
</intent-filter>
</receiver>
在主活动中,使用此代码启动广播接收器
Intent intent = new Intent("package.action.string");
intent.putExtra("extra", phoneNo); \\ phoneNo is the sent Number
sendBroadcast(intent);
在广播接收器中,请使用此代码
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Receiver", "Broadcast received: " + action);
if(action.equals("package.action.string")){
String state = intent.getExtras().getString("extra");
}
}
注意:不要忘记在清单文件中声明您的广播接收器
<receiver android:name=".SmsReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="package.action.string" />
<!-- and some more actions if you want -->
</intent-filter>
</receiver>
在主活动中,使用此代码启动广播接收器
Intent intent = new Intent("package.action.string");
intent.putExtra("extra", phoneNo); \\ phoneNo is the sent Number
sendBroadcast(intent);
在广播接收器中,请使用此代码
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Receiver", "Broadcast received: " + action);
if(action.equals("package.action.string")){
String state = intent.getExtras().getString("extra");
}
}
注意:不要忘记在清单文件中声明您的广播接收器
<receiver android:name=".SmsReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="package.action.string" />
<!-- and some more actions if you want -->
</intent-filter>
</receiver>
在主活动中,使用此代码启动广播接收器
Intent intent = new Intent("package.action.string");
intent.putExtra("extra", phoneNo); \\ phoneNo is the sent Number
sendBroadcast(intent);
在广播接收器中,请使用此代码
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Receiver", "Broadcast received: " + action);
if(action.equals("package.action.string")){
String state = intent.getExtras().getString("extra");
}
}
注意:不要忘记在清单文件中声明您的广播接收器
<receiver android:name=".SmsReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="package.action.string" />
<!-- and some more actions if you want -->
</intent-filter>
</receiver>
更改此行
String action = intent.getAction();
contact = intent.getExtras().getString("contact");
到
换行
String action = intent.getAction();
contact = intent.getExtras().getString("contact");
到
换行
String action = intent.getAction();
contact = intent.getExtras().getString("contact");
到
换行
String action = intent.getAction();
contact = intent.getExtras().getString("contact");
到
确保
phNo
为字符串类型。如果没有,请在广播接收器中使用getInt()
您可能希望在extras中添加一个字符串
,然后在字符串
中检索回来。
如果phNo
变量的类型为int,则应仅以int
的形式检索额外值。确保phNo
为字符串类型。如果没有,请在广播接收器中使用getInt()
您可能希望在extras中添加一个字符串
,然后在字符串
中检索回来。
如果phNo
变量的类型为int,则应仅以int
的形式检索额外值。确保phNo
为字符串类型。如果没有,请在广播接收器中使用getInt()
您可能希望在extras中添加一个字符串
,然后在字符串
中检索回来。
如果phNo
变量的类型为int,则应仅以int
的形式检索额外值。确保phNo
为字符串类型。如果没有,请在广播接收器中使用getInt()
您可能希望在extras中添加一个字符串
,然后在字符串
中检索回来。
如果phNo
变量的类型为int,则应仅以int
的形式检索额外值。尝试以下操作:
Log.e("i am printing my cotnact " , contact );
给我一些反馈
希望有帮助 尝试以下操作:
Log.e("i am printing my cotnact " , contact );
给我一些反馈
希望有帮助 尝试以下操作:
Log.e("i am printing my cotnact " , contact );
给我一些反馈
希望有帮助 尝试以下操作:
Log.e("i am printing my cotnact " , contact );
给我一些反馈
希望有帮助 您没有在日志中打印联系人?是的,我想在日志中获得联系人显示声明phNo
变量的行。获取空指针例外您没有在日志中打印联系人?是的,我想在日志中获得联系人显示声明phNo
变量的行。获取空指针例外您没有在日志中打印您的联系人吗?是的,我想在日志中获取联系人显示声明phNo
变量的行。它获取空指针异常您没有在日志中获取您的联系人吗?是的,我想在日志中获取联系人显示声明phNo
变量的行。它获取空指针异常