android - App fails after launch since trying to implement a checkbox -
at place work have samsung tablets run in kiosk mode.
sadly there bug hardware means if battery gets low loose data , time , can't connect wifi, created simple app in android studio utilizing "logon activity" template settings , correct time issue.
that works well, though able use app settings mdm system.
so tried adding checkbox , if statement "if checked x else y" since doing app won't run.
i've tried looking @ various tutorials , adapting them achieve though far have had no luck.
my code below if amazing.
ps : i'm not android developer , i'am picking bits google. have lot learn sorry if missed simple.
loginactivity.java
package mypackagename; import android.animation.animator; import android.animation.animatorlisteneradapter; import android.annotation.targetapi; import android.content.intent; import android.content.pm.packagemanager; import android.support.annotation.nonnull; import android.support.design.widget.snackbar; import android.support.v7.app.appcompatactivity; import android.app.loadermanager.loadercallbacks; import android.content.cursorloader; import android.content.loader; import android.database.cursor; import android.net.uri; import android.os.asynctask; import android.os.build; import android.os.bundle; import android.provider.contactscontract; import android.text.textutils; import android.view.keyevent; import android.view.view; import android.view.view.onclicklistener; import android.view.inputmethod.editorinfo; import android.widget.arrayadapter; import android.widget.autocompletetextview; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.checkbox; import android.widget.toast; import java.util.arraylist; import java.util.list; import android.content.context; import android.telephony.telephonymanager; import static android.manifest.permission.read_contacts; /** * login screen offers login via email/password. */ public class loginactivity extends appcompatactivity implements loadercallbacks<cursor> { telephonymanager tel; textview imei; /** * id identity read_contacts permission request. */ private static final int request_read_contacts = 0; /** * dummy authentication store containing known user names , passwords. * todo: remove after connecting real authentication system. */ private static final string[] dummy_credentials = new string[]{ "user@domain.domain:password" }; /** * keep track of login task ensure can cancel if requested. */ private userlogintask mauthtask = null; // ui references. private autocompletetextview memailview; private edittext mpasswordview; private view mprogressview; private view mloginformview; private checkbox cb1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); // set login form. memailview = (autocompletetextview) findviewbyid(r.id.email); populateautocomplete(); mpasswordview = (edittext) findviewbyid(r.id.password); mpasswordview.setoneditoractionlistener(new textview.oneditoractionlistener() { @override public boolean oneditoraction(textview textview, int id, keyevent keyevent) { if (id == r.id.login || id == editorinfo.ime_null) { attemptlogin(); return true; } return false; } }); button memailsigninbutton = (button) findviewbyid(r.id.email_sign_in_button); memailsigninbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { attemptlogin(); } }); mloginformview = findviewbyid(r.id.login_form); mprogressview = findviewbyid(r.id.login_progress); tel = (telephonymanager) getsystemservice(context.telephony_service); imei = (textview) findviewbyid(r.id.textview2); imei.settext(tel.getdeviceid().tostring()); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_login); } private void populateautocomplete() { if (!mayrequestcontacts()) { return; } getloadermanager().initloader(0, null, this); } private boolean mayrequestcontacts() { if (build.version.sdk_int < build.version_codes.m) { return true; } if (checkselfpermission(read_contacts) == packagemanager.permission_granted) { return true; } if (shouldshowrequestpermissionrationale(read_contacts)) { snackbar.make(memailview, r.string.permission_rationale, snackbar.length_indefinite) .setaction(android.r.string.ok, new view.onclicklistener() { @override @targetapi(build.version_codes.m) public void onclick(view v) { requestpermissions(new string[]{read_contacts}, request_read_contacts); } }); } else { requestpermissions(new string[]{read_contacts}, request_read_contacts); } return false; } /** * callback received when permissions request has been completed. */ @override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { if (requestcode == request_read_contacts) { if (grantresults.length == 1 && grantresults[0] == packagemanager.permission_granted) { populateautocomplete(); } } } /** * attempts sign in or register account specified login form. * if there form errors (invalid email, missing fields, etc.), * errors presented , no actual login attempt made. */ private void attemptlogin() { if (mauthtask != null) { return; } // reset errors. memailview.seterror(null); mpasswordview.seterror(null); // store values @ time of login attempt. string email = memailview.gettext().tostring(); string password = mpasswordview.gettext().tostring(); boolean cancel = false; view focusview = null; // check valid password, if user entered one. if (!textutils.isempty(password) && !ispasswordvalid(password)) { mpasswordview.seterror(getstring(r.string.error_invalid_password)); focusview = mpasswordview; cancel = true; } // check valid email address. if (textutils.isempty(email)) { memailview.seterror(getstring(r.string.error_field_required)); focusview = memailview; cancel = true; } else if (!isemailvalid(email)) { memailview.seterror(getstring(r.string.error_invalid_email)); focusview = memailview; cancel = true; } if (cancel) { // there error; don't attempt login , focus first // form field error. focusview.requestfocus(); } else { // show progress spinner, , kick off background task // perform user login attempt. showprogress(true); mauthtask = new userlogintask(email, password); mauthtask.execute((void) null); } } private boolean isemailvalid(string email) { //todo: replace own logic return email.contains("@"); } private boolean ispasswordvalid(string password) { //todo: replace own logic return password.length() > 4; } /** * shows progress ui , hides login form. */ @targetapi(build.version_codes.honeycomb_mr2) private void showprogress(final boolean show) { // on honeycomb mr2 have viewpropertyanimator apis, allow // easy animations. if available, use these apis fade-in // progress spinner. if (build.version.sdk_int >= build.version_codes.honeycomb_mr2) { int shortanimtime = getresources().getinteger(android.r.integer.config_shortanimtime); mloginformview.setvisibility(show ? view.gone : view.visible); mloginformview.animate().setduration(shortanimtime).alpha( show ? 0 : 1).setlistener(new animatorlisteneradapter() { @override public void onanimationend(animator animation) { mloginformview.setvisibility(show ? view.gone : view.visible); } }); mprogressview.setvisibility(show ? view.visible : view.gone); mprogressview.animate().setduration(shortanimtime).alpha( show ? 1 : 0).setlistener(new animatorlisteneradapter() { @override public void onanimationend(animator animation) { mprogressview.setvisibility(show ? view.visible : view.gone); } }); } else { // viewpropertyanimator apis not available, show // , hide relevant ui components. mprogressview.setvisibility(show ? view.visible : view.gone); mloginformview.setvisibility(show ? view.gone : view.visible); } } @override public loader<cursor> oncreateloader(int i, bundle bundle) { return new cursorloader(this, // retrieve data rows device user's 'profile' contact. uri.withappendedpath(contactscontract.profile.content_uri, contactscontract.contacts.data.content_directory), profilequery.projection, // select email addresses. contactscontract.contacts.data.mimetype + " = ?", new string[]{contactscontract.commondatakinds.email .content_item_type}, // show primary email addresses first. note there won't // primary email address if user hasn't specified one. contactscontract.contacts.data.is_primary + " desc"); } @override public void onloadfinished(loader<cursor> cursorloader, cursor cursor) { list<string> emails = new arraylist<>(); cursor.movetofirst(); while (!cursor.isafterlast()) { emails.add(cursor.getstring(profilequery.address)); cursor.movetonext(); } addemailstoautocomplete(emails); } @override public void onloaderreset(loader<cursor> cursorloader) { } private void addemailstoautocomplete(list<string> emailaddresscollection) { //create adapter tell autocompletetextview show in dropdown list. arrayadapter<string> adapter = new arrayadapter<>(loginactivity.this, android.r.layout.simple_dropdown_item_1line, emailaddresscollection); memailview.setadapter(adapter); } private interface profilequery { string[] projection = { contactscontract.commondatakinds.email.address, contactscontract.commondatakinds.email.is_primary, }; int address = 0; int is_primary = 1; } /** * represents asynchronous login/registration task used authenticate * user. */ public class userlogintask extends asynctask<void, void, boolean> { private final string memail; private final string mpassword; userlogintask(string email, string password) { memail = email; mpassword = password; } @override protected boolean doinbackground(void... params) { // todo: attempt authentication against network service. try { // simulate network access. thread.sleep(2000); } catch (interruptedexception e) { return false; } (string credential : dummy_credentials) { string[] pieces = credential.split(":"); if (pieces[0].equals(memail)) { // // account exists, return true if password matches. return pieces[1].equals(mpassword); } } // todo: register new account here. return false; } @override protected void onpostexecute(final boolean success) { mauthtask = null; showprogress(false); //success below if (success) { //initiate check box checkbox cb1 = (checkbox) findviewbyid(r.id.cb1); //check current state of check box (true or false) boolean checkboxstate = cb1.ischecked(); if(cb1.ischecked()) { intent = getpackagemanager().getlaunchintentforpackage("com.mobileiron"); i.addflags(intent.flag_activity_new_task); i.setpackage("com.mobileiron"); i.setpackage(null); startactivity(i); finish(); } else { intent = new intent(android.provider.settings.action_date_settings); i.addflags(intent.flag_activity_new_task); i.setpackage("android.provider.settings.action_date_settings"); i.setpackage(null); startactivity(i); finish(); } } else { mpasswordview.seterror(getstring(r.string.error_incorrect_password)); mpasswordview.requestfocus(); } } @override protected void oncancelled() { mauthtask = null; showprogress(false); } } } </i>`
checkbox activity_login.xml
<checkbox android:text="@string/cb1" android:layout_width="328dp" android:layout_height="wrap_content" android:id="@+id/cb1" android:onclick="oncheckboxclicked" android:checked="false" />
your oncheckboxclicked method onclick of checkbox not found in activity.
Comments
Post a Comment