开发者

How can we call an activity through service in android?

I want to know if it is possible to call an activity through background service in android like :

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.开发者_如何学编程media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;



public class background extends Service{

 private int timer1;
 @Override
 public void onCreate() {
  // TODO Auto-generated method stub
  super.onCreate();

  SharedPreferences preferences = getSharedPreferences("SaveTime", MODE_PRIVATE);
  timer1 = preferences.getInt("time", 0);
  startservice();
 }

 @Override
 public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
 }

 private void startservice() {

  Handler handler = new Handler();
  handler.postDelayed(new Runnable(){
   public void run() {
    mediaPlayerPlay.sendEmptyMessage(0);
   }
  }, timer1*60*1000);
 }

 private Handler mediaPlayerPlay = new Handler(){

  @Override
  public void handleMessage(Message msg) {
   try
   {
    getApplication();
    MediaPlayer mp = new MediaPlayer();

    mp = MediaPlayer.create(background.this, R.raw.alarm);
    mp.start();

   }
   catch(Exception e)
   {
    e.printStackTrace();
   }
   super.handleMessage(msg);
  }
 };


 /*
  * (non-Javadoc)
  * 
  * @see android.app.Service#onDestroy()
  */
 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub

  super.onDestroy();
 }
}

i want to call my activity......


You can call an Activity while onStart() of your service.....

Snippet might be as follows:

@Override 
public void onStart(Intent intent, int startId)  { 
...
Log.i("Service", "onStart() is called"); 
Intent callIntent = new Intent(Intent.ACTION_CALL); 
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
callIntent.setClass(<Set your package name and class name here>);
startActivity(callIntent);
...

}


I believe launching user-interactive Activity from a non-interactive Service goes against the design of Android, in that it would pull out control from under the user.

Notifications are the mechanism intended to get user's attention from a background app, and give them an opportunity to launch the interactive Activity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜