0

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

我现在对处理一种新型(不确定这是否是正确的词)应用程序很感兴趣,并且想知道我应该如何去做。我不确定要研究什么,并希望您的建议能帮助我顺利进行。我会告诉你我的想法。

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

我当然不希望有一个答案告诉我如何准确地做到这一点,我只需要一些指导,因为我以前从未编写过这种性质的应用程序。最重要的是,是否有可能做我想做的事?

谢谢你。

4

3 回答 3

3

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

以下示例取自 android 博客,它是Service类的实现

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来实现,IntentService 是按需处理异步请求的服务的基类。

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) {
              }
          }
      }
  }
}

You can also go through SO post https://stackoverflow.com/a/4353653/432903

于 2012-08-21T03:19:57.153 回答
2

如果您的应用程序主要不是用 javascript/webview/phonegap 编写的,那么您所要做的就是查看 Service 类。该课程和链接的文档会告诉您您需要知道的一切。

于 2012-08-21T03:03:22.023 回答
1

maybe you can use an IntentFilter so you can get a system notify when the user uses a dialer. and you should learn the Service component which can work in background in android.

于 2012-08-21T03:22:03.237 回答