这是我们项目唯一尚未解决的问题。假设该应用已安装在某人的手机上。然后我们想通过向我/她发送一条与我们程序中的消息的body参数匹配的消息来启动应用程序。我遵循了一些代码,但似乎所有我尝试过的代码都没有用。请尊重。感谢那些慷慨帮助的人。
public class SmsReceiver extends BroadcastReceiver{
public static String trigger = "";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str="";
String num="";
Intent i = new Intent();
i.setClassName("gavadev.com.autolaunch", "gavadev.com.autolaunch.NewAct");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if(bundle != null){
Object[] pdus = (Object[]) bundle.get("pdus");
msgs= new SmsMessage[pdus.length];
for(int j =0 ;j<pdus.length; j++) {
trigger = msgs[j].getMessageBody().toString();
num = msgs[j].getOriginatingAddress();
str += trigger;
}
Toast.makeText(context,"From: "+num+ " Message: " +trigger,Toast.LENGTH_LONG).show();
if((trigger.equals("themessage"))&&(num.equals("thenumber"))){
context.startActivity(i);
}
}
}
}
这是我的Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gavadev.com.autolaunch">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".NewAct"
android:label="@string/title_activity_new"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
</manifest>
*我在MainActivity.class上没有做任何事情