android - 我们如何通过服务在android中调用一个 Activity ?

标签 android

我想知道是否可以通过 android 中的后台服务调用 Activity :

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();
 }
}

我想调用我的 Activity ......

最佳答案

您可以在服务的 onStart() 时调用 Activity ......

fragment 可能如下:

@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);
...

}

关于android - 我们如何通过服务在android中调用一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2544214/

相关文章:

android - 在 gradle 中设置 applicationId 以实现组合产品 flavor

java - 无法构建cordova项目

java - 创建自定义搜索栏

android - 有没有办法用 ArrayAdapter 更新多个 TextView?

android - 具有未烘焙纹理的 3D 对象在 metaio sdk 中无法正确呈现

UI线程上的Android Junit4测试

android - 如何跟踪 Intent

android - 无法获取在 ExpandableListView 中编辑文本中输入的字符串(不是通常的 Focus 问题)

java - Android Google +1 按钮问题

java - 在应用程序运行时保留 HashMap 数据 - Android