如何取消设置我的短信应用程序在Android的默认应用程序 [英] How to unset my sms app as default app in Android

查看:234
本文介绍了如何取消设置我的短信应用程序在Android的默认应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想取消设置是否我的SMS应用程序在Android的默认应用程序。我下面这个教程:

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

我可以用下面的code设置我的短信应用程序的默认短信应用程序:

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 .

推荐答案

要取消选择您的应用程序作为默认的短信应用程序,用户必须同意另一个应用程序为默认值。如果存在至少一个应用程序安装,可以作为一个缺省SMS应用程序,系统将需要一个包被指定为这样。这会不会是从用户体验的角度来看理想,但它是必要的,因为奇巧,为默认的短信应用程序无法清除自己的状态为默认值。

To un-select your app as the default SMS app, the user must approve another app as the default. If there is at least one app installed that can act as a default SMS app, the system will require that a package be designated as such. This won't be ideal from a user experience standpoint, but it's necessary since KitKat, as the default SMS app cannot clear its own status as default.

要简化这个过程中,我写了 selectDefaultSmsPackage()方法会发现,接受android.provider.Telephony.SMS_DELIVER所有应用程序广播(不包括当前包),并提示用户使用列表选择。从列表中选择所需的应用程序后,通常是/将不会出现验证对话框。当活动接收结果,所选择的应用程序的包名相比,当前设置为默认值。正如一些用户报告的结果code是不可靠的在此情况下,检查当前的默认为约以保证正确的结果的唯一方法。

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. 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. 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 result.

我们将使用一个自定义的对话框列出显示名称和符合条件的应用程序图标。该活动创建对话框必须实现对话框 OnAppSelectedListener 界面。

We will be using a custom Dialog to list the display names and icons of eligible apps. The Activity creating the Dialog must implement the Dialog's 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);
                }
            }
        );

        final String[] appList = new String[apps.size()];
        for (int i = 0; i < appList.length; i++) {
            appList[i] = apps.get(i).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;
            ...
        }
    }

我们需要以下POJO类来保存相关应用程序的信息。

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;
    }
}

AppsDialog 类创建一个简单的的ListView 的可用默认设置,并通过选择回活动通过该接口。

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;
        }
    }
}

ArrayAdapter 使用下列项目布局, list_item可

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>

这篇关于如何取消设置我的短信应用程序在Android的默认应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆