如何在android中创建多线程?

发布于 2024-10-29 08:22:38 字数 318 浏览 2 评论 0原文

我是 android 新手,我正在做一些可能会使用的应用程序 多线程。 例如,应用程序线程可能会执行以下操作,假设 2 线程;

线程 1 即使整个应用程序在前台线程上运行,也应该 始终运行监听特定短信; 假设发送此消息时要拦截的短信是“3456” 到电话,然后线程一将暂停 并且线程 2 将被激活:

Thread 2 当线程 to 被激活时,它将使用 GPS 来跟踪位置 手机的,并将使用 smsManager 的实例发回 手机的坐标(日志,纬度),或者如果可能的话谷歌地图回来 发送消息“3456”的电话,然后线程一将被 激活:

**如何实现这一点?

I am new to android and i was doing some application which might use
multi threading.
For example the application threads might do as follows assuming 2
threads;

Thread 1
Even if the overall application runs on foreground thread one should
run at all times listening for specific sms;
Imagine the sms to be Intercepted is "3456" when this message is sent
to the phone then thread one will be paused
and thread 2 will be activated:

Thread 2
When thread to is activated then it will use gps to track the location
of the phone and will use instance of smsManager to send back the
coordinates(log, lat) of the phone or even if possible google map back
to the phone which sent the message "3456" and then thread one will be
activated:

**How to make this happen any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱你一生 2024-11-05 08:22:38

这个问题有两个答案。

  1. 如果您想在后台运行一个线程很长一段时间,以监听事件或运行常规进程,那么服务是正确的选择

  2. 如果您需要启动一个新线程来做一些处理,然后停止,然后看看 AsyncTask,这是一个非常非常简单的方法,并且包括一个在过程中更新用户界面的简单方法。

开发者文档包含关于Android 中的位置

以下是一些有关在应用中接收短信的信息

There are two answers to this question.

  1. If you want to run a thread in the background over a long period of time, to listen for events or run a regular process then Services are the way to go

  2. If you need to fire off a new thread to do some processing once and then stop, then look at AsyncTask which is a very, very easy way to do that, and includes a simple way to update the user interface during the process.

The developer docs contain an excellent page about location in Android

Here's some information about receiving SMS in your app

蓝天白云 2024-11-05 08:22:38

看看服务。如果您在应用程序中使用服务,则许多内容不必显式编码。

http://developer.android.com/reference/android/app/Service.html

编辑

为了回应评论,以下是我如何使用 BroadcastReceiver

public class SomeActivity extends Activity {

BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        // Example of pulling a string out of the Intent's "extras"
        String msg = intent.getStringExtra("BroadcastString");

        // ...more stuff

    }
};


@Override
public void onResume()
{
    super.onResume();
    registerReceiver(receiver, new IntentFilter("SomeStringKeyForMyBroadcast"));

    // ... other stuff
}

@Override
public void onPause()
{
    super.onPause();
    unregisterReceiver(receiver);

    // ... other stuff
}

并在我的服务中从服务到活动进行通信......

public class SomeService extends Service {

Intent broadcastIntent = new Intent("SomeStringKeyForMyBroadcast");

private void someWorkerMethodInMyService()
{
        // ... other stuff

        broadcastIntent.putExtra("BroadcastString", "Some Data");
        sendBroadcast(broadcastIntent);

        // ... other stuff
}

类似的东西......

Have a look at Services. A lot of this does not have to be explicitly coded if you make use of Services in your application.

http://developer.android.com/reference/android/app/Service.html

Edit

In response to comments, here's how I do communication from Service to Activity using BroadcastReceiver

public class SomeActivity extends Activity {

BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        // Example of pulling a string out of the Intent's "extras"
        String msg = intent.getStringExtra("BroadcastString");

        // ...more stuff

    }
};


@Override
public void onResume()
{
    super.onResume();
    registerReceiver(receiver, new IntentFilter("SomeStringKeyForMyBroadcast"));

    // ... other stuff
}

@Override
public void onPause()
{
    super.onPause();
    unregisterReceiver(receiver);

    // ... other stuff
}

and in my Service...

public class SomeService extends Service {

Intent broadcastIntent = new Intent("SomeStringKeyForMyBroadcast");

private void someWorkerMethodInMyService()
{
        // ... other stuff

        broadcastIntent.putExtra("BroadcastString", "Some Data");
        sendBroadcast(broadcastIntent);

        // ... other stuff
}

something like that...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文