开始 Activity 接收短信

Start Activity on Receiving SMS

我想运行ActivityIwanttoRun.classActivity收到包含"Run activity"[=的短信46=].. if 语句在收到包含文本 "RunActivity".

时吐司消息

但是在ActivityIwanttoRun.class中不显示Toast "You have called your second activity"语句--

谁能告诉我这里出了什么问题..我只想看看我的 Activity - “ActivityIwanttoRun” 运行在收到短信时。

IncomingSms.java

public class IncomingSms extends BroadcastReceiver  {

final SmsManager sms = SmsManager.getDefault();
@Override
public void onReceive(Context context, Intent intent) {
    final Bundle bundle = intent.getExtras();

    try {

        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();

                    if(message.contains("RunActivity")) {

                    int duration = Toast.LENGTH_LONG;
                    Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                    Intent intent2 = new Intent(context,ActivityIwanttoRun.class);
                    context.startActivity(intent2);
                    toast.show();
                }
            }
        }

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" +e);

    }
}

}

ActivityIwanttoRun.java

public class ActivityIwanttoRun extends Activity  {
private static final String TAG = "ActivityIwanttoRun";

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    updateUI();
}

 private void updateUI() {

        Toast.makeText(this, "You have called your second activity",Toast.LENGTH_LONG ).show();

}

}

清单

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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>

    <activity android:name=".ActivityIwanttoRun">

    </activity>

    <receiver android:name=".IncomingSms">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

谢谢!

首先您要启动 LocationActivity 而不是 ActivityIwanttoRun

改变

 Intent intent2 = new Intent(context,LocationActivity.class);
                    context.startActivity(intent2);

Intent intent2 = new Intent(context,ActivityIwanttoRun.class);
                    context.startActivity(intent2);

BroadcastReceiver 调用 Activity 的正确方法是

Intent intent2 = new Intent(context,ActivityIwanttoRun.class);
                    context.startActivity(intent2);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);

使用这个

Intent in = new Intent();

    in.setClassName("package name", "package name.ActivityIwanttoRun");

    in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    context.startActivity(in);

并提及互联网并作为清单的子项接收短信许可