Creating and using IntentService

package in.co.wizardinfosolutions.servicetest;
import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }
    @Override    protected void onHandleIntent(@Nullable Intent intent) {
        for(int i=1;i<=10;i++)
        {
            showToast("Number is "+i);
            try{Thread.sleep(1000);}catch(Exception ex){}
        }
    }
    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.servicetest;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {

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

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


Comments

  1. If you're looking to find blockchain developers, I would suggest checking some of the online platforms. Whether you are looking to grow your in-house staff, hire blockchain developer on a freelance basis, or contract a consulting firm, here are several ideas to help you blockchain hiring companies.

    ReplyDelete

Post a Comment

Popular posts from this blog

Using JSON Object with PHP

Using SQLite with Android