Skip to content

Instantly share code, notes, and snippets.

@vivdub
Created June 15, 2011 05:11
Show Gist options
  • Save vivdub/22310506c1c2a1aa81df to your computer and use it in GitHub Desktop.
Save vivdub/22310506c1c2a1aa81df to your computer and use it in GitHub Desktop.
This file demonstrates how to start activity from service
package com.test;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
public static boolean RUNNING = false;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
RUNNING = true;
new Thread(){public void run(){
try{
Thread.sleep(5000);
Intent i = new Intent(MyService.this, UpdateDBProgressDialog.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyService.this.startActivity(i);
}catch(Exception e){e.printStackTrace();}
}}.start();
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
RUNNING = false;
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment