Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.service_m;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.widget.Toast;
- public class ServiceCode extends Service{
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- //TODO do something useful
- for(int i=0;i<=10;i++){
- Toast.makeText(getApplicationContext(), "hello world",
- Toast.LENGTH_LONG).show();
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return Service.START_NOT_STICKY;
- }
- }
- package com.example.service_m;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- public class ServiceStarter extends BroadcastReceiver{
- @Override
- public void onReceive(Context _context, Intent _intent) {
- Intent i = new Intent("com.example.service_m");
- i.setClass(_context, ServiceCode.class);
- _context.startService(i);
- }
- }
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.service_m.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- </intent-filter>
- </activity>
- <receiver android:name="com.example.service_m.ServiceStarter" >
- <intent-filter >
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- </intent-filter>
- </receiver>
- </application>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement