如果用户已经注册,则直接启动Activity

时间:2015-09-16 12:18:36

标签: android google-cloud-messaging

我是android开发的新手。如果用户已经注册,我希望我的应用程序直接启动MainActivity。我怎样才能做到这一点。 这是我的MainActivity

  public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
Button btnTip, btnApp, btndos, btnAbout, btnServices;
ConnectionDetector cd;
AsyncTask<Void, Void, Void> mRegisterTask;
public static String name;
public static String email;
public static String contact;
public static String imei;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.app_bar);
    toolbar.setTitle("Dental Application");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    btnTip = (Button) findViewById(R.id.tips);
    btndos = (Button) findViewById(R.id.dos);
    btnApp = (Button) findViewById(R.id.appointments);
    btnAbout = (Button) findViewById(R.id.about);
    btnServices = (Button) findViewById(R.id.services);
    // Alert dialog manager
    AlertDialogManager alert = new AlertDialogManager();
    cd = new ConnectionDetector(getApplicationContext());

    // Check if Internet present
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
        alert.showAlertDialog(MainActivity.this,
                "Internet Connection Error",
                "Please check your Internet connection", false);
        // stop executing code by return
        return;
    }

    Intent i = getIntent();

    name = i.getStringExtra("name");
    email = i.getStringExtra("email");
    contact = i.getStringExtra("contact");
    imei = i.getStringExtra("imei");
    // Make sure the device has the proper dependencies.
    GCMRegistrar.checkDevice(this);

    // Make sure the manifest was properly set - comment out this line
    // while developing the app, then uncomment it when it's ready.
    GCMRegistrar.checkManifest(this);

    //lblMessage = (TextView) findViewById(R.id.lblMessage);

    registerReceiver(mHandleMessageReceiver, new IntentFilter(
            DISPLAY_MESSAGE_ACTION));

    // Get GCM registration id
    final String regId = GCMRegistrar.getRegistrationId(this);

    // Check if regid already presents
    if (regId.equals("")) {
        // Registration is not present, register now with GCM
        GCMRegistrar.register(this, SENDER_ID);
    } else {
        // Device is already registered on GCM
        if (GCMRegistrar.isRegisteredOnServer(this)) {
            // Skips registration.
            Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
        } else {
            // Try to register again, but not in the UI thread.
            // It's also necessary to cancel the thread onDestroy(),
            // hence the use of AsyncTask instead of a raw thread.
            final Context context = this;
            mRegisterTask = new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {
                    // Register on our server
                    // On server creates a new user
                    ServerUtilities.register(context, name, email, regId, contact, imei);
                    return null;
                }

                @Override
                protected void onPostExecute(Void result) {
                    mRegisterTask = null;
                }

            };
            mRegisterTask.execute(null, null, null);
        }
    }

    btnTip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, TipsActivity.class);
            startActivity(intent);
        }
    });

    btndos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, DosActivity.class);
            startActivity(intent);
        }
    });

    btnApp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, BookAppointmennts.class);
            startActivity(intent);
        }
    });

    btnAbout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, AboutUsActivity.class);
            startActivity(intent);
        }
    });
    btnServices.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, ServicesActivity.class);
            startActivity(intent);
        }
    });

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

/**
 * Receiving push messages
 * */
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
        // Waking up mobile if it is sleeping
        WakeLocker.acquire(getApplicationContext());

        /**
         * Take appropriate action on this message
         * depending upon your app requirement
         * For now i am just displaying it on the screen
         * */

        // Showing received message
        //lblMessage.append(newMessage + "\n");
        Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();

        // Releasing wake lock
        WakeLocker.release();
    }
};

@Override
protected void onDestroy() {
    if (mRegisterTask != null) {
        mRegisterTask.cancel(true);
    }
    try {
        unregisterReceiver(mHandleMessageReceiver);
        GCMRegistrar.onDestroy(this);
    } catch (Exception e) {
        Log.e("UnRegister Receiver", "> " + e.getMessage());
    }
    super.onDestroy();
}

}

和RegisterActivity

  public class RegisterActivity extends Activity {
// alert dialog manager
AlertDialogManager alert = new AlertDialogManager();

// Internet detector
ConnectionDetector cd;

// UI elements
EditText txtName;
EditText txtEmail;
EditText txtContact;

// Register button
Button btnRegister;
String imei;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);


    cd = new ConnectionDetector(getApplicationContext());

    // Check if Internet present
    if (!cd.isConnectingToInternet()) {
        // Internet Connection is not present
        alert.showAlertDialog(RegisterActivity.this,
                "Internet Connection Error",
                "Please connect to working Internet connection", false);
        // stop executing code by return
        return;
    }

    // Check if GCM configuration is set
    if (SERVER_URL == null || SENDER_ID == null || SERVER_URL.length() == 0
            || SENDER_ID.length() == 0) {
        // GCM sernder id / server url is missing
        alert.showAlertDialog(RegisterActivity.this, "Configuration Error!",
                "Please set your Server URL and GCM Sender ID", false);
        // stop executing code by return
        return;
    }

    txtName = (EditText) findViewById(R.id.txtName);
    txtEmail = (EditText) findViewById(R.id.txtEmail);
    txtContact = (EditText) findViewById(R.id.contact);
    btnRegister = (Button) findViewById(R.id.btnRegister);
    TelephonyManager mngr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    imei = mngr.getDeviceId();
    /*
     * Click event on Register button
     * */
    btnRegister.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Read EditText dat
            String name = txtName.getText().toString();
            String email = txtEmail.getText().toString();
            String contact = txtContact.getText().toString();
            // Check if user filled the form
            if (name.trim().length() > 0 && email.trim().length() > 0 && contact.trim().length()>0) {
                // Launch Main Activity
                Intent i = new Intent(getApplicationContext(), MainActivity.class);

                // Registering user on our server
                // Sending registraiton details to MainActivity
                i.putExtra("name", name);
                i.putExtra("email", email);
                i.putExtra("contact", contact);
                i.putExtra("imei", imei);
                startActivity(i);
                finish();
            } else {
                // user doen't filled that data
                // ask him to fill the form
                alert.showAlertDialog(RegisterActivity.this, "Registration Error!", "Please enter your details", false);
            }
        }
    });
}

 }

我正在使用GCM。用户首次注册并显示MainActivity。下次当用户打开应用程序时,我希望直接显示MainActivity。我怎样才能做到这一点。谁能帮帮我吗。

2 个答案:

答案 0 :(得分:1)

你必须像SplashScreen一样制作LauncherActivity,其中检查用户已经注册的共享prefrences或sqlite数据 然后通过检查此转移到相应的活动。

如果用户未注册,则显示注册屏幕,当用户注册时,然后以Sqlite或共享偏好或任何其他方式保存信息。

如果用户已注册show HomeScreen

答案 1 :(得分:0)

首先,只发布需要修改的代码,您已经在您的Java文件中发布了所有代码。如果您的代码没有混乱,我们可能会更有帮助。