要設爲系統默認的短信應用首先要配置一下AndroidManifest.xml文件,添加下列:android
<!-- 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了,以下:app
<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的服務,把這幾個文件建立出來就能夠了,暫時不用作什麼操做less
設爲系統默認短信的關鍵在ComposeSmsActivity.activity中,以下:ide
@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);
}
}
好了,設置系統默認的短信應用就行了!ui