首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:如何开发在“后台”运行的应用程序?

Android:如何开发在“后台”运行的应用程序?
EN

Stack Overflow用户
提问于 2012-08-21 10:51:00
回答 3查看 226关注 0票数 0

我习惯于开发独立的应用程序,当你点击它,它就会运行,当你完成后,你就退出了。

我现在对处理一种新类型的应用程序很感兴趣(不确定这是不是合适的词),并且想知道我应该如何去做。我不确定要研究什么,并感谢您的建议,以帮助我开始滚动。我会告诉你我的想法。

我的应用程序需要在拨号器中执行一个特殊操作。当用户拨打一个号码并且正在通话时,我希望用户能够按下菜单键,并找到一个选项来滚动他们所有的联系人(无论是股票应用程序,还是我从手机中存储的联系人中抓取的我自己的列表),然后选择一个。选择后,该联系人的号码将粘贴到拨号器中(请记住,在呼叫过程中)。

我当然不期望答案告诉我如何做到这一点,我只是需要一些指导,因为我从来没有写过这样的应用程序。最重要的是,有没有可能做我想做的事情?

谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-21 11:19:57

你需要通过Android ServiceIntentServiceService是一个应用程序组件,可以在后台执行长时间运行的操作,并且不提供用户界面(UI)。

下面的示例取自android blog,它是Service类的实现

代码语言:javascript
复制
public class HelloService extends Service {
  private Looper mServiceLooper;
  private ServiceHandler mServiceHandler;

  // Handler that receives messages from the thread
  private final class ServiceHandler extends Handler {
      public ServiceHandler(Looper looper) {
          super(looper);
      }
      @Override
      public void handleMessage(Message msg) {
          // Normally we would do some work here, like download a file.
          // For our sample, we just sleep for 5 seconds.
          long endTime = System.currentTimeMillis() + 5*1000;
          while (System.currentTimeMillis() < endTime) {
              synchronized (this) {
                  try {
                      wait(endTime - System.currentTimeMillis());
                  } catch (Exception e) {
                  }
              }
          }
          // Stop the service using the startId, so that we don't stop
          // the service in the middle of handling another job
          stopSelf(msg.arg1);
      }
  }

  @Override
  public void onCreate() {
    // Start up the thread running the service.  Note that we create a
    // separate thread because the service normally runs in the process's
    // main thread, which we don't want to block.  We also make it
    // background priority so CPU-intensive work will not disrupt our UI.
    HandlerThread thread = new HandlerThread("ServiceStartArguments",
            Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    // Get the HandlerThread's Looper and use it for our Handler 
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

      // For each start request, send a message to start a job and deliver the
      // start ID so we know which request we're stopping when we finish the job
      Message msg = mServiceHandler.obtainMessage();
      msg.arg1 = startId;
      mServiceHandler.sendMessage(msg);

      // If we get killed, after returning from here, restart
      return START_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
      // We don't provide binding, so return null
      return null;
  }

  @Override
  public void onDestroy() {
    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
  }
}

另一方面,也可以使用IntentService实现相同的功能,它是按需处理异步请求的Services基类。

代码语言:javascript
复制
public class HelloIntentService extends IntentService {

  /** 
   * A constructor is required, and must call the super IntentService(String)
   * constructor with a name for the worker thread.
   */
  public HelloIntentService() {
      super("HelloIntentService");
  }

  /**
   * The IntentService calls this method from the default worker thread with
   * the intent that started the service. When this method returns, IntentService
   * stops the service, as appropriate.
   */
  @Override
  protected void onHandleIntent(Intent intent) {
      // Normally we would do some work here, like download a file.
      // For our sample, we just sleep for 5 seconds.
      long endTime = System.currentTimeMillis() + 5*1000;
      while (System.currentTimeMillis() < endTime) {
          synchronized (this) {
              try {
                  wait(endTime - System.currentTimeMillis());
              } catch (Exception e) {
              }
          }
      }
  }
}

你也可以通过SO post https://stackoverflow.com/a/4353653/432903

票数 3
EN

Stack Overflow用户

发布于 2012-08-21 11:03:22

如果您的应用程序主要不是用javascript/webview/phonegap编写的,那么您所要做的就是查看Service类。这个类和链接的文档告诉了您需要了解的所有内容。

票数 2
EN

Stack Overflow用户

发布于 2012-08-21 11:22:03

也许你可以使用IntentFilter,这样当用户使用拨号器时,你就可以得到系统通知。你应该学习在android中可以在后台工作的Service组件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12047848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档