android - startActivityForResult always return 0 when restore sms -
this code working on lollypop version, but, when use on kitkat, returns 0 when choose "yes/no" option dialog.
btnsmsrestore.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { if (currentapiversion >= android.os.build.version_codes.kitkat){ defaultsmsapp = telephony.sms.getdefaultsmspackage(smsactivity.this); if (!getpackagename().equals(defaultsmsapp)) { intent intent = new intent(telephony.sms.intents.action_change_default); intent.putextra(telephony.sms.intents.extra_package_name, getpackagename()); //startactivity(intent); startactivityforresult(intent, def_sms_req); } } else { new restoresms().execute(); } }); @override protected void onactivityresult(int requestcode, int resultcode, intent data) { switch (requestcode) { case def_sms_req: if(resultcode == activity.result_ok) { new restoresms().execute(); } } }
activity.result_ok value -1 , resultcode in onactivityresult 0 when use kitkat.
from android documentation:
onactivityresult called when activity launched exits, giving requestcode started with, resultcode returned, , additional data it. resultcode result_canceled if activity explicitly returned that, didn't return result, or crashed during operation.
result_canceled
value 0, on kitkat not set result value , default 1 returned.
as workaround on kitkat, can try check if app default 1 when onactivityresult fired. try code:
btnsmsrestore.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { if (build.version.sdk_int >= android.os.build.version_codes.kitkat) { if (!isdefaultsmsapp(smsactivity.this)) { { intent intent = new intent(telephony.sms.intents.action_change_default); intent.putextra(telephony.sms.intents.extra_package_name, getpackagename()); startactivityforresult(intent, def_sms_req); } } else { new restoresms().execute(); } }); @override protected void onactivityresult(int requestcode, int resultcode, intent data) { switch (requestcode) { case def_sms_req: if (build.version.sdk_int == android.os.build.version_codes.kitkat && isdefaultsmsapp(this) || build.version.sdk_int >= build.version_codes.lollipop && resultcode == activity.result_ok) { new restoresms().execute(); } break; } } @targetapi(build.version_codes.kitkat) public static boolean isdefaultsmsapp(context context) { return context.getpackagename().equals(telephony.sms.getdefaultsmspackage(context)); }
Comments
Post a Comment