如何将一个广播接收器和另一个活动实现为Launcher活动

时间:2014-07-06 10:00:18

标签: android android-activity android-manifest

我有两个发射器活动,
1.作为广播接收器的接收器活动 2.应该是主要启动器活动的活动。

当SMS到达时,接收者活动启动.MainActivity(BroadcastReceiver),并进一步启动一项服务。 (没有GUI)并且没有打开Settings活动 另一项活动.Settings是主要启动器活动。

目标我想要Achive:
当短信到达时receiver活动按原样运行。但是当我想要更改设置Setting活动开始时。

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aa"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.aa.FindLocation"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.aa.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.aa.Settings"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.aa.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".MainActivity"
        android:enabled="false" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </receiver>

    <service android:name=".Servc" >
        <intent-filter>
            <action android:name="com.example.Servc" />
        </intent-filter>
    </service>
</application>

</manifest>

问题
1
    [2014-07-06 02:55:14 - Aa]将Aa.apk上传到设备&#39; S5830f4524b76&#39;
    [2014-07-06 02:55:14 - Aa]安装Aa.apk ...
    [2014-07-06 02:55:18 - Aa]成功!
    [2014-07-06 02:55:18 - Aa]启动活动com.example.aa.Settings on device S5830f4524b76
    [2014-07-06 02:55:19 - Aa] ActivityManager:开始:意图{act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] cmp = com.example.aa / .Settings }
2
应该在清单中做出哪些改变才能使其有效?

1 个答案:

答案 0 :(得分:0)

不要在清单中定义你的短信接收器。删除它。

通过在清单中定义以下元素来创建在启动时运行的广播接收器:

<receiver android:name=".BootUpReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

您需要将此权限添加到清单中:

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

然后在那里注册接收BroadcastReceiver的短信:

public class BootUpReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        SmsReceiver smsreceiver = new SmsReceiver();        
        IntentFilter fltr_smsreceived = new IntentFilter(
                    "android.provider.Telephony.SMS_RECEIVED");
            registerReceiver(smsreceiver, fltr_smsreceived);
        }
    }

SmsReceiver是您的广播接收器。例如:

public class SmsReceiver extends BroadcastReceiver {

    void setActivityHandler(){
        //instantiate member variables if required
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle pdusBundle = intent.getExtras();
        Object[] pdus=(Object[])pdusBundle.get("pdus");
        SmsMessage messages=SmsMessage.createFromPdu((byte[]) pdus[0]);
        Log.d("tag","message body : " + messages.getMessageBody());
    }


}