How to set my application as default to receive SMS

老子叫甜甜 提交于 2019-12-01 00:58:12

How to set your app as default messaging app?

Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, YOUR_PACKAGE_NAME); 

How to check if your app is default messaging app?

@TargetApi(Build.VERSION_CODES.KITKAT) public static boolean isDefaultSmsApp(Context context) {     return context.getPackageName().equals(Telephony.Sms.getDefaultSmsPackage(context)); } 

From the preference activity add OnPreferenceClickListener and add the following code inside it which first check if it is already a default messaging app, then opens a screen where the user can change it else set the current app as the default messaging app.

 if (isDefaultSmsApp(getActivity())) {                         startActivityForResult(new Intent(Settings.ACTION_WIRELESS_SETTINGS), 0);                     } else {                     final String packageName = getActivity().getPackageName();                         Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);                         intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName);                         startActivityForResult(intent, 0);                     } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!