两个使用设备之间的短信交流 [英] communication between two device using sms

查看:123
本文介绍了两个使用设备之间的短信交流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要我的程序能够从一个特殊号码(+ 9856874236)接收短信。

我想申请成为recive短信,如果短信是含有启用无线,然后更改无线网络启用

我使用的这个code,但它的工作崩溃?

MainActivity

 包com.example.sms;

进口android.app.Activity;
进口android.content.Context;
进口android.net.wifi.WifiManager;
进口android.os.Bundle;

进口android.view.Menu;
进口android.widget.TextView;

公共类MainActivity延伸活动{
    静态TextView的消息框;


    静态字符串x =;
    静态上下文的背景下;
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

         消息框=(TextView中)findViewById(R.id.messageBox);
         检查();

    }


    //如何使用的其他或多个活动



    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }


     公共静态无效updateMessageBox(弦乐味精,从字符串)
        {
            messageBox.append(MSG);
            //从数据库中父母的电话号码
            //检查父号码与...
            如果(msg.equals(启用无线))
            {
                X =是;
                MainActivity objSampleClass =新MainActivity();
                objSampleClass.check();
            }
            //context.startActivity(new意图(上下文,MainActivity.class));

            //意图I =新的意图(背景下,p1.class);
            //context.startActivity(i);

          }

     公共无效检查()
     {

         如果(x.equals(是))
         {
             WifiManager无线=(WifiManager)getSystemService(Context.WIFI_SERVICE);
              wifi.setWifiEnabled(真正的);
         }
         其他
         {

         }
     }

}
 

清单

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.sms
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =8
        机器人:targetSdkVersion =17/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=com.example.sms.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;

        <接收器的Andr​​oid版本:NAME =com.example.sms.TextMessageReceiver>
            <意向滤光器>
                <作用机器人:名称=android.provider.Telephony.SMS_RECEIVED/>
            &所述; /意图滤光器>
        < /接收器>


        <活动
            机器人:名称=com.example.sms.Test
            机器人:标签=@字符串/ title_activity_test>
        < /活性GT;

    < /用途>

    <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_SMS/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_WIFI_STATE>

    < /使用-许可>
    <使用-权限的Andr​​oid:名称=android.permission.BLUETOOTH/>
<使用-权限的Andr​​oid:名称=android.permission.BLUETOOTH_ADMIN/>
    <使用-权限的Andr​​oid:名称=android.permission.CHANGE_WIFI_STATE>
    < /使用-许可>

< /舱单>
 

解决方案

要尽早拦截的短信我用下面的code:

 公共类通讯扩展服务{

私人最终字符串变量= this.getClass()getSimpleName()。

私人SMSReceiver mSMSreceiver;
私人的IntentFilter mIntentFilter;

@覆盖
公共无效的onCreate()
{
    super.onCreate();


    Log.i(TAG,通讯开始);
    //短信事件接收器
    mSMSreceiver =新SMSReceiver();
    mIntentFilter =新的IntentFilter();
    mIntentFilter.addAction(android.provider.Telephony.SMS_RECEIVED);
    mIntentFilter.setPriority(2147483647);
    registerReceiver(mSMSreceiver,mIntentFilter);

    意向意图=新的意图(android.provider.Telephony.SMS_RECEIVED);
    名单< ResolveInfo>的相关信息= getPackageManager()queryBroadcastReceivers(意向,0)。
    对于(ResolveInfo信息:相关信息){
        Log.i(TAG,收货人姓名:+ info.activityInfo.name +;优先级=+ info.priority);
    }
}

@覆盖
公共无效的onDestroy()
{
    super.onDestroy();

    //注销短信接收器
    unregisterReceiver(mSMSreceiver);
}

@覆盖
公众的IBinder onBind(意向为arg0){
    返回null;
}

私有类SMSReceiver扩展的BroadcastReceiver
{
    私人最终字符串变量= this.getClass()getSimpleName()。

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图)
    {
        捆绑额外= intent.getExtras();


        字符串strMessage =;

        如果(临时演员!= NULL)
        {
            [对象] smsextras =(对象[])extras.get(的PDU);

            的for(int i = 0; I< smsextras.length;我++)
            {
                SmsMessage smsmsg = SmsMessage.createFromPdu((字节[])smsextras [I]);

                。字符串strMsgBody = smsmsg.getMessageBody()的toString();
                串strMsgSrc = smsmsg.getOriginatingAddress();

                strMessage + = + strMsgSrc +的短信:+ strMsgBody;

                Log.i(TAG,strMessage);

                如果(strMsgBody.contains(SomeText)){


                            //执行code

                    this.abortBroadcast();

                }
            }



        }

    }

}
}
 

记住清单中注册该服务:

 <服务机器人:名称=yourpackage.Communicator/>
 

现在的onCreate和的OnDestroy启动和停止服务:

  startService(新意图(这一点,Communicator.class));
stopService(新意图(这一点,Communicator.class));
 

I want my program to be able to receive sms from a special number("+9856874236").

I want application to be recive SMS ,if the SMS is contain "enable wifi" ,then change wifi to enable

i 'm using this code but it working crash??

MainActivity

package com.example.sms;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;

import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    static TextView messageBox;


    static String x="";
    static Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         messageBox=(TextView)findViewById(R.id.messageBox);
         check();

    }


    //How to used other or many activity



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


     public static void updateMessageBox(String msg,String from)
        {
            messageBox.append(msg);
            //Get parent phone number from database
            //check parent number with ...
            if(msg.equals("enable wifi"))
            {
                x="yes";
                MainActivity objSampleClass = new MainActivity();  
                objSampleClass.check();  
            }
            //context.startActivity(new Intent(context, MainActivity.class));

            //Intent i=new Intent(context, p1.class);
            //context.startActivity(i);

          }

     public void check()
     {

         if(x.equals("yes"))
         {
             WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
              wifi.setWifiEnabled(true);
         }
         else
         {

         }
     }

}

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sms"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sms.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.example.sms.TextMessageReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>


        <activity
            android:name="com.example.sms.Test"
            android:label="@string/title_activity_test" >
        </activity>

    </application>

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >

    </uses-permission>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
    </uses-permission>

</manifest>

解决方案

To intercept the SMS as early as possible I use the following code:

public class Communicator extends Service {

private final String TAG = this.getClass().getSimpleName();

private SMSReceiver mSMSreceiver;
private IntentFilter mIntentFilter;

@Override
public void onCreate()
{
    super.onCreate();


    Log.i(TAG, "Communicator started");
    //SMS event receiver
    mSMSreceiver = new SMSReceiver();
    mIntentFilter = new IntentFilter();
    mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
    mIntentFilter.setPriority(2147483647);
    registerReceiver(mSMSreceiver, mIntentFilter);

    Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVED");
    List<ResolveInfo> infos = getPackageManager().queryBroadcastReceivers(intent, 0);
    for (ResolveInfo info : infos) {
        Log.i(TAG, "Receiver name:" + info.activityInfo.name + "; priority=" + info.priority);
    }
}

@Override
public void onDestroy()
{
    super.onDestroy();

    // Unregister the SMS receiver
    unregisterReceiver(mSMSreceiver);
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

private class SMSReceiver extends BroadcastReceiver
{
    private final String TAG = this.getClass().getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Bundle extras = intent.getExtras();


        String strMessage = "";

        if ( extras != null )
        {
            Object[] smsextras = (Object[]) extras.get( "pdus" );

            for ( int i = 0; i < smsextras.length; i++ )
            {
                SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]);

                String strMsgBody = smsmsg.getMessageBody().toString();
                String strMsgSrc = smsmsg.getOriginatingAddress();

                strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;                    

                Log.i(TAG, strMessage);

                if (strMsgBody.contains("SomeText")) {


                            // perform code

                    this.abortBroadcast();

                }
            }



        }

    }

}
}

Remember to register the service in manifest:

<service android:name="yourpackage.Communicator" />

Now in onCreate and onDestroy start and stop the service:

startService(new Intent(this, Communicator.class));
stopService(new Intent(this, Communicator.class));

这篇关于两个使用设备之间的短信交流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆