在过去的几个小时里,我一直在查看有关该主题的其他问题,但我发现没有一个能够给我任何答案。
在后台将字符串从活动传递到广播接收器的最佳方法是什么?
这是我的主要活动
public class AppActivity extends DroidGap {
SmsReceiver mSmsReceiver = new SmsReceiver();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scroll;
scroll = new ScrollView(this);
Bundle bundle = getIntent().getExtras();
final String ownAddress = bundle.getString("variable");
registerReceiver(mSmsReceiver, new IntentFilter("MyReceiver"));
Intent intent = new Intent("MyReceiver");
intent.putExtra("passAddress", ownAddress);
sendBroadcast(intent);
Log.v("Example", "ownAddress: " + ownAddress);
}
}
这是我的广播接收器
public class AppReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
final String ownAddress = intent.getStringExtra("passAddress");
Toast test = Toast.makeText(context,""+ownAddress,Toast.LENGTH_LONG);
test.show();
Log.v("Example", "ownAddress: " + ownAddress);
}
}
这是我的接收器的清单
<service android:name=".MyService" android:enabled="true"/>
<receiver android:name="AppReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_SENT"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<receiver>
<service android:name=".MyServiceSentReceived" android:enabled="true"/>
<receiver android:name="AppReceiver">
<intent-filter android:priority="2147483645">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
当广播接收器记录一个事件时,应用程序崩溃。我需要让它在幕后运行,并从我的主要活动中拉出一根绳子。
任何人都可以帮助我或指出正确的方向吗?