我习惯开发独立的应用程序,你点击它们,运行它们,当你完成后,你就退出了。
我现在有兴趣处理应用程序的新类型(不确定这是否是正确的词),并且想知道我应该怎么做。我不确定要研究什么,并且非常感谢你的建议,以帮助我开球。我会告诉你我的想法。
我的应用需要在拨号器中执行特殊操作。当用户拨打号码并且正在通话中时,我希望用户能够按菜单键,并找到一个选项以滚动浏览所有联系人(股票应用程序或我自己的列表)我从存储在手机中的联系人中抓取,然后选择一个。选择后,该联系人的号码将被粘贴到拨号器中(请记住,在通话过程中)。
我当然不希望答案告诉我如何准确地做到这一点,我只需要一些指导,因为我以前从未写过这种性质的应用程序。最重要的是,甚至可以做我想做的事情吗?
谢谢。
答案 0 :(得分:3)
您需要浏览Android Service或IntentService。 Service是一个应用程序组件,可以在后台执行长时间运行的操作,但不提供用户界面(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可以实现同样的目的,Services是https://stackoverflow.com/a/4353653/432903的基类,可以按需处理异步请求。
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 {{3}}
答案 1 :(得分:2)
如果您的应用主要不是用javascript / webview / phonegap编写的,那么您只需查看Service类即可。该课程和链接的文件告诉您需要知道的一切。
答案 2 :(得分:1)
也许你可以使用IntentFilter,这样你就可以在用户使用拨号器时获得系统通知。 你应该学习可以在android中的后台工作的服务组件。