收到短信后启动android Activity

时间:2015-07-16 19:31:33

标签: android

应用程序启动并在后台作为服务运行。 我想从任何电话号码

收到特定文本后运行活动

接收SMS java类,如下所示,

ReceiveSMS.java

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,SimpleActivity.class);
                intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intentHome);

            }

        }



}

}

简单活动类如下

public class SimpleActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

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

}

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidautostartup"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" >
</uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".BootComplete"
android:enabled="true"
android:exported="false" >
<intent-filter android:priority="99999">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".ReceiveSMS">
<intent-filter android:priority="99999">
<action android:name="android.provider.telephony.SMS_RECIEVED"></action>
</intent-filter>
</receiver>
<service android:name=".AutoStartUp" >
</service>
<activity android:name="com.example.androidautostartup">   
</activity>
<activity android:name="com.example.androidautostartup.SimpleActivity">
</activity>
<activity
android:name="com.example.androidautostartup.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>
</application>
</manifest>

请任何人都可以解决这个问题,它确实在收到&#34; START&#34;之后进行简单的活动。短信。

1 个答案:

答案 0 :(得分:1)

您需要将SimpleActivity添加到AndroidManifest。