Android的 - 得到改变默认的SMS应用程序对话结果应用程序、结果、Android、SMS

由网友(伱已经卜爱我ㄋ)分享简介:我工作的奇巧恢复短信。参照此文章我已经加入这需要设置我的应用程序作为默认的短信应用程序中的事情。将所有需要的东西清单文件后,我写了下面的code:I am working on restoring SMS on KITKAT. Referring to this article I have added the th...

我工作的奇巧恢复短信。参照此文章我已经加入这需要设置我的应用程序作为默认的短信应用程序中的事情。将所有需要的东西清单文件后,我写了下面的code:

I am working on restoring SMS on KITKAT. Referring to this article I have added the things which are required to set my app as default app for SMS. After adding all required things in manifest file I have write the following code:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
    mDefaultSmsApp = Telephony.Sms.getDefaultSmsPackage(mContext);
    Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
    intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, mContext.getPackageName());
    mContext.startActivity(intent);
}

以上code显示了该对话框,但我无法从这项活动/对话中使用的用户点击是得到的结果还是没有,因为我想添加监听器或得到任何code,应重新present用户点击这些按钮。 谢谢你。

The above code shows this dialog but I am unable to get the result from this activity/dialog either user clicked on Yes or No because I want to add listener or get any code which should represent that the user clicked on these buttons. Thanks.

推荐答案

要做到这一点的方法之一是火与 startActivityForResult的意图(),然后检查 onActivityResult()方法结果code 。请注意,我已经改变了code的例子,在一个活动的上下文中运行。

One way to do this is to fire the Intent with startActivityForResult(), and then check the resultCode in the onActivityResult() method. Please note that I've changed the code in the example to run in an Activity's Context.

private static final int DEF_SMS_REQ = 0;
private String mDefaultSmsApp;

...

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    {
        mDefaultSmsApp = Telephony.Sms.getDefaultSmsPackage(this);

        if (!getPackageName().equals(mDefaultSmsApp))
        {
            Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getPackageName());
            startActivityForResult(intent, DEF_SMS_REQ);
        }
    }       

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch (requestCode)
    {
        case DEF_SMS_REQ:           
            boolean isDefault = resultCode == Activity.RESULT_OK;
            ...
    }
}

正如在下面评论,显然检查结果code不是100%的可靠。一个更安全的检查是简单地比较你的应用程序的包名称为当前默认 onActivityResult()。有没有必要检查结果code可言,就像在注释显示链接的答案。

As mentioned in a comment below, apparently checking the result code is not 100% reliable. A safer check is to simply compare your app's package name to the current default in onActivityResult(). There's no need to check the result code at all, like the answer linked in the comment shows.

String currentDefault = Sms.getDefaultSmsPackage(this);
boolean isDefault = getPackageName().equals(currentDefault);
阅读全文

相关推荐

最新文章