Tuesday 3 March 2015

splash activity

package com.cssoft.speedlocker;

import static com.cssoft.speedlocker.CommonUtilities.DISPLAY_MESSAGE_ACTION;
import static com.cssoft.speedlocker.CommonUtilities.EXTRA_MESSAGE;
import static com.cssoft.speedlocker.CommonUtilities.SENDER_ID;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.cssoft.fonts.MyFonts;
import com.cssoft.sharedpref.GetSharedPref;
import com.cssoft.sharedpref.SetSharedPref;
import com.cssoft.speedlocker.R;
import com.google.android.gcm.GCMRegistrar;

public class SplashActivity extends Activity {

AsyncTask<Void, Void, Void> mRegisterTask;
private TextView txt_loading;

@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);

registerDevice();

/*
* new Handler().postDelayed(new Runnable() {
*
* @Override public void run() { registerDevice();
*
* } }, 5000);
*/

getUI();
startIntent();
}

private void getUI() {
txt_loading = (TextView) findViewById(R.id.loading);
txt_loading.setTypeface(MyFonts.AirStrike(this));
}

int count = 1;

private void startIntent() {

new Timer().scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {

runOnUiThread(new Runnable() {
public void run() {
if (count == 1) {
txt_loading.setText("Loading.");
count = 2;
} else if (count == 2) {
txt_loading.setText("Loading..");
count = 3;
} else if (count == 3) {
txt_loading.setText("Loading...");
cancel();
}
}
});

}
}, 0, 1000);

new Handler().postDelayed(new Runnable() {

@Override
public void run() {
int id = GetSharedPref.getUserID(SplashActivity.this);
if (id > 0) {
startActivity(new Intent(SplashActivity.this,
SliderActivity.class));
overridePendingTransition(R.anim.zoomin, R.anim.fadeout);
finish();
} else {
startActivity(new Intent(SplashActivity.this,
ChooseActivity.class));
overridePendingTransition(R.anim.zoomin, R.anim.fadeout);
finish();
}

}
}, 3000);

}

public void registerDevice() {

// 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);

registerReceiver(mHandleMessageReceiver, new IntentFilter(
DISPLAY_MESSAGE_ACTION));

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

if (!regId.isEmpty()) {
SetSharedPref.setGCMID(SplashActivity.this, regId);
Toast.makeText(getApplicationContext(), regId, Toast.LENGTH_SHORT)
.show();
}

System.out.println("id: " + regId);

// 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

return null;
}

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

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

}

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

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 Error", "> " + e.getMessage());
}
super.onDestroy();
}
}

No comments:

Post a Comment