简体   繁体   English

如何在 Android 中取消将我的短信应用设置为默认应用

[英]How to unset my sms app as default app in Android

I want to unset whether my SMS app as default app in android.我想取消设置我的 SMS 应用程序是否为 android 中的默认应用程序。 I am following this tutorial:我正在关注本教程:

http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html 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 .从该应用程序中,我可以将我的短信应用程序取消设置为默认设置。

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.要取消选择您的应用程序作为默认 SMS 应用程序,您可以让用户选择另一个符合条件的应用程序作为默认应用程序代替您的应用程序,并使用该包名称触发ACTION_CHANGE_DEFAULT Intent

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.为了简化过程,我编写了selectDefaultSmsPackage()方法,该方法将查找所有接受"android.provider.Telephony.SMS_DELIVER"广播的应用程序(不包括当前包),并提示用户一个列表可供选择。 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.Activity收到结果时,会将所选应用程序的包名称与当前设置的默认值进行比较以确定成功。 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.我们将使用自定义Dialog来列出符合条件的应用程序的显示名称和图标。 The Activity creating the AppsDialog must implement its OnAppSelectedListener interface.创建AppsDialogActivity必须实现其OnAppSelectedListener接口。

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.我们需要以下 POJO 类来保存相关的应用程序信息。

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. AppsDialog类创建一个可用默认值的简单ListView ,并通过接口将选择传递回Activity

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 . ArrayAdapter使用以下项目布局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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM