如何在Android中接收到的SMS活动启动 [英] How to start Activity over incoming sms in android

查看:118
本文介绍了如何在Android中接收到的SMS活动启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重新开始传入SMS.For一个活动,我已经做到了这一点......

 公共类ReceiveSMS扩展广播接收器{    布尔SendSMS;
    串Mobileno;
    串VarMessageBody;    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        捆绑包= intent.getExtras();
        SmsMessage []封邮件= NULL;
        字符串str =;
        如果(捆绑!= NULL){
            [对象]的PDU =(对象[])bundle.get(的PDU);
            封邮件=新SmsMessage [pdus.length]
            的for(int i = 0; I< msgs.length;我++){
                封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
                STR + =短信来自+封邮件[I] .getOriginatingAddress();
                Mobileno =封邮件[I] .getOriginatingAddress();
                STR + =;
                STR + =封邮件[I] .getMessageBody()的toString()。
                。VarMessageBody =封邮件[I] .getMessageBody()的toString();
                STR + =\\ n;
                Mobileno =封邮件[I] .getOriginatingAddress();            }
            如果(VarMessageBody.startsWith(START)){
                意图int​​entHome =新意图(背景下,SwitchBluetoothActivity.class);
                intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intentHome);            }        }    }
}

和我SwitchBluetoothActivity类是这样的..

 公共类SwitchBluetoothActivity延伸活动{
    私人TextView的电视;
    私人切换按钮TB;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        电视=(的TextView)findViewById(R.id.bluetoothText);
        TB =(切换按钮)findViewById(R.id.toggleButton);        最后BluetoothAdapter BTA = BluetoothAdapter.getDefaultAdapter();        如果(BTA == NULL){
            tv.setText(设备不支持蓝牙);
        }
        否则如果(bta.isEnabled()){
            tv.setText(蓝牙已启用);
            tb.setChecked(真);
        }
        其他{
            tv.setText(蓝牙已禁用);
            tb.setChecked(假);
        }        tb.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
              如果(bta.isEnabled()){
                    bta.disable();
                tv.setText(蓝牙已禁用);
                }
                其他{
                    bta.enable();
                tv.setText(蓝牙已启用);
                }
            }
        });
    }
}

在AndroidManifest.xml

和我添加了一些意图过滤also.But我的问题是。我不能够启动这个activity.Please给我建议,在那里我做了错误的。

下面是我的清单文件

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.xxxxxx.smsServices
    安卓版code =1
    机器人:=的versionName1.0>
    <活动机器人:名字=。SwitchBluetoothActivity>    < /活性GT;
    <接收机器人:ReceiveSMSNAME =>
        &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.MAIN/>
        <类机器人:名字=android.intent.category.LAUNCHER/>
      &所述; /意图滤光器>
    < /接收器>    <采用-SDK安卓的minSdkVersion =8/>
    <使用许可权的android:NAME =android.permission.BROADCAST_SMS/>
    <使用许可权的android:NAME =android.permission.BLUETOOTH/>
    <使用许可权的android:NAME =android.permission.BLUETOOTH_ADMIN/>
    <使用许可权的android:NAME =android.permission.RECEIVE_SMS/>
    <使用许可权的android:NAME =android.permission.RECEIVE_SMS/>    <应用
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME>
    < /用途>< /清单>


解决方案

默认没有任何优先级的广播接收器,让launches.for启动您acitvity您在接收器中添加优先默认的活动清单

 <接收机器人:名字=。SmsReciever>
        <意向过滤器的android:优先=99999>
            <作用机器人:名字=android.provider.telephony.SMS_RECIEVED>< /作用>
        &所述; /意图滤光器>
    < /接收器>

也u必须在清单中添加这个permision

 <使用许可权的android:NAME =android.permission.RECEIVE_SMS>< /使用许可权>

i want to start an Activity over incoming SMS.For that I have done this...

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

            }

        }

    }
}

and my SwitchBluetoothActivity class is like this..

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

And i have added intent filter in AndroidManifest.xml also.But my problem is . i am not able to start this activity.Please give me suggestion where i did the mistake.

Here is my manifest file

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxxxxx.smsServices"
    android:versionCode="1"
    android:versionName="1.0" >
    <activity android:name=".SwitchBluetoothActivity">

    </activity>
    <receiver android:name=".ReceiveSMS">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </receiver>

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.BROADCAST_SMS"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
    </application>

</manifest>

解决方案

default your broadcast receiver not having any priority so the default activity launches.for launching your acitvity add priority for your receiver in manifest

      <receiver android:name=".SmsReciever">
        <intent-filter android:priority="99999">
            <action android:name="android.provider.telephony.SMS_RECIEVED"></action>
        </intent-filter>
    </receiver>

also u have to add this permision in manifest

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

这篇关于如何在Android中接收到的SMS活动启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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