Starting an Activity with Toast from IntentService

package in.co.wizardinfosolutions.batch270705;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.os.Looper;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.widget.Toast;


/** * An {@link IntentService} subclass for handling asynchronous task requests in * a service on a separate handler thread. * <p>
 * TODO: Customize class - update intent actions, extra parameters and static * helper methods. */public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }
    @Override    protected void onHandleIntent(@Nullable Intent intent) {

            try{Thread.sleep(3000);}catch(Exception ex){}
            showToast("Starting Main Activity");
            Intent ma = new Intent(getBaseContext(), MainActivity.class);
            ma.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplication().startActivity(ma);
    }
    protected void showToast(final String msg){
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override            public void run() {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}


package in.co.wizardinfosolutions.batch270705;
import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.Toast;
public class SplashActivity extends AppCompatActivity {

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

        Intent s = new Intent(this, MyIntentService.class);
        startService(s);
    }
}

Comments

Popular posts from this blog

Creating and using IntentService

Using JSON Object with PHP

Using SQLite with Android