IntentServices with Example in Android

Here is source code of the Program to Demonstrate IntentServices in Android. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

Main Activity

package com.example.intentservices;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button start = (Button) findViewById(R.id.start);
        final Intent intent = new Intent(this, MyIntentService.class);
        start.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startService(intent);
            }
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}

MyIntentService

package com.example.intentservices;
 
import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;
 
public class MyIntentService extends IntentService {
 
    public MyIntentService(String name) {
        super("MyIntentService");
        // TODO Auto-generated constructor stub
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stubted
        Toast.makeText(this, "Intent Service Created", Toast.LENGTH_LONG)
                .show();
    }
 
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "Intent Service Started", Toast.LENGTH_LONG)
                .show();
 
        super.onCreate();
    }
 
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
 
}

Screenshot_2013-12-16-19-39-40[1]

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

advertisement
advertisement
If you wish to look at all Tutorials, go to Java Android Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.