Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中通过传入短信启动服务_Android - Fatal编程技术网

在android中通过传入短信启动服务

在android中通过传入短信启动服务,android,Android,根据我的要求,我希望通过传入的SMS启动/停止服务 按照迪内什的想法,我做了这件事 public class ReceiveSMS extends BroadcastReceiver { Boolean SendSMS; String Mobileno; String VarMessageBody; @Override public void onReceive(Context context, Intent intent) { Bun

根据我的要求,我希望通过传入的SMS启动/停止服务

按照迪内什的想法,我做了这件事

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);

            }

        }

    }

但我无法启动此活动。

注册一个广播接收器,当短信传入时,该接收器将被接收。您可以启动该服务。

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

有关启动和停止服务,请参阅


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

在项目中注册BroadcastReceiver类收听传入的sms教程和下面的代码链接

广播接收器类收听接收短信后开始活动示例代码如下:

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();
            }
    }



}
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.sms消息;
导入android.widget.Toast;
公共类ReceiveSMS扩展广播接收器
{
MapMarker obj_gps=新MapMarker();
布尔发送;
字符串Mobileno;
字符串体;
@凌驾
公共void onReceive(上下文、意图)
{
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的SMS消息---
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];

for(int i=0;iDear Dinesh。谢谢你的回答。回答得很好,伙计。你好@Dinesh今天我尝试了你的给定代码,但无法启动活动。你在android清单文件中注册了BroadcastReceiver吗?
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();
            }
    }



}