根据我的要求,我想通过传入短信启动/停止服务。
根据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");
}
}
});
}
}
但我无法开始这项活动。
答案 0 :(得分:0)
注册一个BroadcastReceiver,当Sms传入时。你可以开始服务。
答案 1 :(得分:0)
您可以将广播接收器用于传入短信 并且在那个接收器中你可以把你的启动和停止服务的逻辑基于短信的内容,你还必须设置服务逻辑在设备关闭和重启后保持启动,为此你可以使用设备重启的广播接收器
启动和停止服务请参考
http://developer.android.com/reference/android/app/Service.html
答案 2 :(得分:-1)
在您的项目中注册BroadcastReceiver类,听取传入的短信教程和代码链接
http://www.androidcompetencycenter.com/2008/12/android-api-sms-handling/
在广播接收器类侦听接收短信到开始活动示例代码之后:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class ReceiveSMS extends BroadcastReceiver
{
MapMarker obj_gps = new MapMarker();
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)
{
//---retrieve the SMS message received---
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();
}
//---display the new SMS message---
if (VarMessageBody.startsWith("START"))//Check Message Body String Start with START
Intent intentHome = new Intent(context, HomeScreen.class);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intentHome.putExtra("No",Mobileno);
//intentHome.putExtra("Msg",VarMessageBody);
//intentHome.putExtra("Mob", Mobileno);
context.startActivity(intentHome);
}
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
此代码接收发送短信格式“开始”开始活动..!
注意:我是Start Activity,只有在条件允许的情况下开始服务......!