Reputation: 2897
I want to unset whether my SMS app as default app in android. I am following this tutorial:
http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html
I can set my SMS app as default SMS app by the following code:
Intent intent = new Intent(context, Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName());
startActivity(intent);
But I want to unset my SMS app as default app. How can I do that?
Here One point is to be noted : I have installed messaging classic app . From that app , I can unset my sms app as default .
Upvotes: 5
Views: 861
Reputation: 39191
Before you bother reading through this answer, you might have a look at this much simpler option (that I wish I'd've thought of before working this one). This answer is still handy if you'd like to let the user choose which app to set, rather than allowing the system to revert to whichever it decides.
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.
public class MainActivity extends Activity
implements AppsDialog.OnAppSelectedListener {
...
private static final int DEF_SMS_REQ = 0;
private AppInfo selectedApp;
private void selectDefaultSmsPackage() {
final List<ResolveInfo> receivers = getPackageManager().
queryBroadcastReceivers(new Intent(Sms.Intents.SMS_DELIVER_ACTION), 0);
final ArrayList<AppInfo> apps = new ArrayList<>();
for (ResolveInfo info : receivers) {
final String packageName = info.activityInfo.packageName;
if (!packageName.equals(getPackageName())) {
final String appName = getPackageManager()
.getApplicationLabel(info.activityInfo.applicationInfo)
.toString();
final Drawable icon = getPackageManager()
.getApplicationIcon(info.activityInfo.applicationInfo);
apps.add(new AppInfo(packageName, appName, icon));
}
}
Collections.sort(apps, new Comparator<AppInfo>() {
@Override
public int compare(AppInfo app1, AppInfo app2) {
return app1.appName.compareTo(app2.appName);
}
}
);
new AppsDialog(this, apps).show();
}
@Override
public void onAppSelected(AppInfo selectedApp) {
this.selectedApp = selectedApp;
Intent intent = new Intent(Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, selectedApp.packageName);
startActivityForResult(intent, DEF_SMS_REQ);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case DEF_SMS_REQ:
String currentDefault = Sms.getDefaultSmsPackage(this);
boolean isDefault = selectedApp.packageName.equals(currentDefault);
String msg = 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.
public class AppInfo {
String appName;
String packageName;
Drawable icon;
public AppInfo(String packageName, String appName, Drawable icon) {
this.packageName = packageName;
this.appName = appName;
this.icon = icon;
}
@Override
public String toString() {
return appName;
}
}
The AppsDialog
class creates a simple ListView
of the available defaults, and passes the selection back to the Activity
through the interface.
public class AppsDialog extends Dialog
implements OnItemClickListener {
public interface OnAppSelectedListener {
public void onAppSelected(AppInfo selectedApp);
}
private final Context context;
private final List<AppInfo> apps;
public AppsDialog(Context context, List<AppInfo> apps) {
super(context);
if (!(context instanceof OnAppSelectedListener)) {
throw new IllegalArgumentException(
"Activity must implement OnAppSelectedListener interface");
}
this.context = context;
this.apps = apps;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Select default SMS app");
final ListView listView = new ListView(context);
listView.setAdapter(new AppsAdapter(context, apps));
listView.setOnItemClickListener(this);
setContentView(listView);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
((OnAppSelectedListener) context).onAppSelected(apps.get(position));
dismiss();
}
private class AppsAdapter extends ArrayAdapter<AppInfo> {
public AppsAdapter(Context context, List<AppInfo> list) {
super(context, R.layout.list_item, R.id.text, list);
}
public View getView(int position, View convertView, ViewGroup parent) {
final AppInfo item = getItem(position);
View v = 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
.
<LinearLayout xmlns: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">
<ImageView android:id="@+id/icon"
android:layout_width="36dp"
android:layout_height="36dp" />
<TextView android: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>
Upvotes: 6