Android设为系统默认的短信应用

要设为系统默认的短信应用首先要配置一下AndroidManifest.xml文件,添加下列:

<!-- BroadcastReceiver that listens for incoming SMS messages -->
        <receiver android:name=".demo.SmsReceiver"
                  android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>
        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver android:name=".demo.MmsReceiver"
                  android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>
        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name=".demo.ComposeSmsActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </activity>
        <!-- Service that delivers messages from the phone "quick response" -->
        <service android:name=".demo.HeadlessSmsSendService"
                 android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
                 android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>

其中ComposeSmsActivity.activity可以作为启动的Activity,我的是将Main.activity作为启动Activity的,那就要用Main.activity代替ComposeSmsActivity.activity了,如下:

 <activity android:name=".demo.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
</activity>

至于SmsReceiver,MmsReceiver还继承BroadcastReceiver的广播,HeadlessSmsSendService是继承Service的服务,把这几个文件创建出来就可以了,暂时不用做什么操作

设为系统默认短信的关键在ComposeSmsActivity.activity中,如下:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        String defaultSmsApp = null;
  String currentPn = getPackageName();//获取当前程序包名
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT)
{
defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(this);//获取手机当前设置的默认短信应用的包名
}
if (!defaultSmsApp.equals(currentPn)) {
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, currentPn);
startActivity(intent);
}
 }

好了,设置系统默认的短信应用就好了!

posted @ 2016-07-25 10:15  MaTiZi  阅读(9262)  评论(0编辑  收藏  举报