简体   繁体   中英

How to add code in AndroidManifest.xml

I am fairly new to Android programming and I'm doing my best to understand the tutorial. I have downloaded the sample code from this link .

Now I want to integrate the code from this thread . It says here to "declare the SMS receiver in your AndroidManifest.xml"

The code is:

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

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

Here's the existing code from the sample file I downloaded under AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.smsTest"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SMSTest"
                  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>
     <uses-permission android:name="android.permission.SEND_SMS">
    </uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS">
    </uses-permission>
</manifest> 

Now my question is where to add the code (the first code above) into AndroidManifest.xml.

I tried to change this line:

<action android:name="android.intent.action.MAIN" />

with this:

<action android:name="android.provider.Telephony.SMS_RECEIVED"/>

without luck.

Any help is appreciated.

You need to add the receiver as a 2nd item inside the application. You don't replace the intent filter on the activity. It should go between the and tags.

The manifest is really describing whats in your apk. Your application will have 1 activity, SMSTest, that is launched from the launcher. It also has 1 receiver which receives the intent SMS_RECEIVED. So both parts need to be in there. Make sense?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.smsTest"
  android:versionCode="1"
  android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".SMSTest"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<receiver android:name="mypackage.SMSReceiver">
  <intent-filter>
  <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
 <uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM