How To Unset My Sms App As Default App In Android
Solution 1:
To un-select your app as the default SMS app, you can let the user choose another eligible app to act as the default in place of yours, and fire the ACTION_CHANGE_DEFAULT
Intent
with that package name.
To simplify the process, I wrote the selectDefaultSmsPackage()
method that will find all apps that accept the "android.provider.Telephony.SMS_DELIVER"
broadcast (excluding the current package), and prompt the user with a list to choose from. This is a rather naive filtering criterion, but the worst that should happen is that a selected app just won't be successfully set as the default.
After selecting the desired app from the list, the usual yes/no verification dialog will appear. When the Activity
receives the result, the selected app's package name is compared to that currently set as default to determine success. As some users have reported that the result code is not reliable in this case, checking the current default is about the only way to guarantee a correct success result.
We will be using a custom Dialog
to list the display names and icons of eligible apps. The Activity
creating the AppsDialog
must implement its OnAppSelectedListener
interface.
publicclassMainActivityextendsActivityimplementsAppsDialog.OnAppSelectedListener {
...
privatestaticfinalintDEF_SMS_REQ=0;
private AppInfo selectedApp;
privatevoidselectDefaultSmsPackage() {
final List<ResolveInfo> receivers = getPackageManager().
queryBroadcastReceivers(newIntent(Sms.Intents.SMS_DELIVER_ACTION), 0);
final ArrayList<AppInfo> apps = newArrayList<>();
for (ResolveInfo info : receivers) {
finalStringpackageName= info.activityInfo.packageName;
if (!packageName.equals(getPackageName())) {
finalStringappName= getPackageManager()
.getApplicationLabel(info.activityInfo.applicationInfo)
.toString();
finalDrawableicon= getPackageManager()
.getApplicationIcon(info.activityInfo.applicationInfo);
apps.add(newAppInfo(packageName, appName, icon));
}
}
Collections.sort(apps, newComparator<AppInfo>() {
@Overridepublicintcompare(AppInfo app1, AppInfo app2) {
return app1.appName.compareTo(app2.appName);
}
}
);
newAppsDialog(this, apps).show();
}
@OverridepublicvoidonAppSelected(AppInfo selectedApp) {
this.selectedApp = selectedApp;
Intentintent=newIntent(Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, selectedApp.packageName);
startActivityForResult(intent, DEF_SMS_REQ);
}
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case DEF_SMS_REQ:
StringcurrentDefault= Sms.getDefaultSmsPackage(this);
booleanisDefault= selectedApp.packageName.equals(currentDefault);
Stringmsg= selectedApp.appName + (isDefault ?
" successfully set as default" :
" not set as default");
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
break;
...
}
}
We need the following POJO class to hold the relevant app information.
publicclassAppInfo {
String appName;
String packageName;
Drawable icon;
publicAppInfo(String packageName, String appName, Drawable icon) {
this.packageName = packageName;
this.appName = appName;
this.icon = icon;
}
@OverridepublicStringtoString() {
return appName;
}
}
The AppsDialog
class creates a simple ListView
of the available defaults, and passes the selection back to the Activity
through the interface.
publicclassAppsDialogextendsDialogimplementsOnItemClickListener {
publicinterfaceOnAppSelectedListener {
publicvoidonAppSelected(AppInfo selectedApp);
}
privatefinal Context context;
privatefinal List<AppInfo> apps;
publicAppsDialog(Context context, List<AppInfo> apps) {
super(context);
if (!(context instanceof OnAppSelectedListener)) {
thrownewIllegalArgumentException(
"Activity must implement OnAppSelectedListener interface");
}
this.context = context;
this.apps = apps;
}
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Select default SMS app");
finalListViewlistView=newListView(context);
listView.setAdapter(newAppsAdapter(context, apps));
listView.setOnItemClickListener(this);
setContentView(listView);
}
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
((OnAppSelectedListener) context).onAppSelected(apps.get(position));
dismiss();
}
privateclassAppsAdapterextendsArrayAdapter<AppInfo> {
publicAppsAdapter(Context context, List<AppInfo> list) {
super(context, R.layout.list_item, R.id.text, list);
}
public View getView(int position, View convertView, ViewGroup parent) {
finalAppInfoitem= getItem(position);
Viewv=super.getView(position, convertView, parent);
((ImageView) v.findViewById(R.id.icon)).setImageDrawable(item.icon);
return v;
}
}
}
The ArrayAdapter
uses the following item layout, list_item
.
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:paddingTop="1dp"android:paddingBottom="1dp"android:paddingStart="8dp"android:paddingEnd="8dp"><ImageViewandroid:id="@+id/icon"android:layout_width="36dp"android:layout_height="36dp" /><TextViewandroid:id="@+id/text"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical"android:textAppearance="?android:attr/textAppearanceListItemSmall"android:paddingStart="?android:attr/listPreferredItemPaddingStart"android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"android:minHeight="?android:attr/listPreferredItemHeightSmall"android:ellipsize="marquee" /></LinearLayout>
Post a Comment for "How To Unset My Sms App As Default App In Android"