如何将文本消息发送到Android并启动应用程序或应用程序活动

时间:2012-05-23 18:27:48

标签: android gps java-web-start

我的问题非常简单,我知道可以执行该功能。我想通过短信开始活动,例如“我在哪里做”这怎么办?请提供任何信息。我认为SMSReceiver BroadcastReceiver的方向正确,但我不确定。

2 个答案:

答案 0 :(得分:1)

查看此人的拦截短信http://imran-android-sms.blogspot.com/2011/03/receive-sms-on-android.html#more

的示例代码

从那里你只想发动你的新活动的意图。

答案 1 :(得分:1)

您需要在AndroidManifest.xml中注册广播接收器,以便接收SMS_RECIVERD广播:

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

在AndroidManifest中添加权限:

<uses-permission android:name="android.permission.RECEIVE_SMS"/>
在广播接收器代码中

启动您的应用程序:

public class SMSReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){      
   //context.startService(new Intent(context, YourService.class));

   //Start activity as:
    Intent intent24 = new Intent(Intent.ACTION_MAIN).addCategory(
    Intent.CATEGORY_LAUNCHER).setClassName("YOUR_PACKAGE_NAME",
    "com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    .addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("YOUR_PACKAGE_NAME",
    "com.YOUR_PACKAGE_NAME..YOURACTIVITY_NAME"));
    context.startActivity(intent24);
   }
 }
}

注意:要从后台启动活动,您需要设置Intent.FLAG_ACTIVITY_NEW_TASKIntent.FLAG_FROM_BACKGROUND标志,以便从后台开始活动。