根据我的要求,我想通过传入的 SMS 启动/停止服务。
根据 Dinesh 的想法,我这样做了。
public class ReceiveSMS extends BroadcastReceiver {
Boolean SendSMS;
String Mobileno;
String VarMessageBody;
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
Mobileno = msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
VarMessageBody = msgs[i].getMessageBody().toString();
str += "\n";
Mobileno = msgs[i].getOriginatingAddress();
}
if (VarMessageBody.startsWith("START")) {
Intent intentHome = new Intent(context,SwitchBluetoothActivity.class);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentHome);
}
}
}
在我的 SwitchBluetoothActivity 中我写了
public class SwitchBluetoothActivity extends Activity {
private TextView tv;
private ToggleButton tb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.bluetoothText);
tb = (ToggleButton) findViewById(R.id.toggleButton);
final BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
if (bta == null) {
tv.setText("Device does not support Bluetooth");
}
else if(bta.isEnabled()){
tv.setText("Bluetooth is enabled");
tb.setChecked(true);
}
else{
tv.setText("Bluetooth is disabled");
tb.setChecked(false);
}
tb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(bta.isEnabled()){
bta.disable();
tv.setText("Bluetooth is disabled");
}
else{
bta.enable();
tv.setText("Bluetooth is enabled");
}
}
});
}
}
但我无法开始这项活动。