0

根据我的要求,我想通过传入的 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");
                }
            }
        });
    }
}

但我无法开始这项活动。

4

3 回答 3

0

注册一个广播接收器,当短信进来时。你可以启动服务。

于 2012-05-03T05:09:42.080 回答
0

您可以将广播接收器用于传入的短信,并且在该接收器中,您可以根据短信内容设置启动和停止服务的逻辑,并且您还必须设置服务逻辑在设备关闭和重新启动后保持启动,您可以使用设备重启的广播接收器

对于启动和停止服务,请参阅此

http://developer.android.com/reference/android/app/Service.html

于 2012-05-03T05:11:48.830 回答
-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 只有你在 if 条件下启动服务......!

于 2012-05-03T05:14:29.660 回答