标签: android-intentservice

Android:如何确定IntentService是否正在运行?

我有一个上传活动,我称之为Intent服务.在那里我正在处理API请求调用.

我希望活动知道服务是否正在运行,以显示上传标签.

我尝试了以下来确定服务是否正在运行:

public void startUploadServiceTask() {
    if (Util.isNetworkAvailable(mContext)) {

        if (!isMyServiceRunning(UploadDriveService.class)) {

                startService(new Intent(mContext,
                        UploadService.class));

            }
        } else {
            Toast.makeText(mContext,
                    "Service is already running.", Toast.LENGTH_SHORT)
                    .show();
        }
    } else {
        Toast.makeText(mContext,
                getString(R.string.please_check_internet_connection),
                Toast.LENGTH_SHORT).show();
    }
}



private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager
            .getRunningServices(Integer.MAX_VALUE)) {
        Log.e("ALL SERVICE", service.service.getClassName().toString());
        if (serviceClass.getName().equals(service.service.getClassName())) {

            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

但是在Activity Manager Running Service Info中,我没有得到我正在运行的意向服务类,所以这总是假的.

我已经使用Broadcast for API调用响应.

我甚至检查了这段代码.

if(startService(someIntent) != null) { 
 Toast.makeText(getBaseContext(), …
Run Code Online (Sandbox Code Playgroud)

service android android-intent activity-manager android-intentservice

6
推荐指数
2
解决办法
9839
查看次数

使用IntentService附近的消息

最初我设置了一个BroadcastReceiver接收来自Nearby Messages API的意图.

class BeaconMessageReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        Nearby.getMessagesClient(context).handleIntent(intent, object : MessageListener() {
            override fun onFound(message: Message) {
                val id = IBeaconId.from(message)
                Timber.i("Found iBeacon=$id")
                sendNotification(context, "Found iBeacon=$id")
            }

            override fun onLost(message: Message) {
                val id = IBeaconId.from(message)
                Timber.i("Lost iBeacon=$id")
                sendNotification(context, "Lost iBeacon=$id")
            }
        })
    }

    private fun sendNotification(context: Context, text: String) {
        Timber.d("Send notification.")
        val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        val notification = NotificationCompat.Builder(context, Notifications.CHANNEL_GENERAL)
                .setContentTitle("Beacons")
                .setContentText(text)
                .setSmallIcon(R.drawable.ic_notification_white)
                .build() …
Run Code Online (Sandbox Code Playgroud)

android android-intentservice android-ble beacon google-nearby

6
推荐指数
1
解决办法
312
查看次数

对于IntentService,START_STICKY

我看过很多android服务示例,其中返回START_STICKY用于启动应用程序,但无论如何我可以使用相同的IntentService.我知道Service方法在主UI线程和IntentService上作为单独的线程运行.

  1. 但究竟如何调用它们以及为什么不能在启动时启动IntentService.由于IntentService在一个单独的线程上运行,所以如果我没有注意,我们可以更多地控制它.

  2. 我尝试在IntentService中使用onStartCommand,但我的应用程序在启动时崩溃,即使它在手动启动时工作正常.我们可以在IntentService中覆盖onStartCommand吗?

有人可以帮我弄这个吗 ?

service android android-service intentservice android-intentservice

5
推荐指数
1
解决办法
7007
查看次数

IntentService - 查找队列中等待的Intents数

在我的应用程序中,我使用IntentService来完成一些工作.我想看看有多少意图正在等待被处理,因为IntentService持有他们的"工作队列",并将在未来一到onStartCommand()作为onStartCommand前一个已完成.

如何在这个"工作队列"中找出有多少意图在等待?

android android-service android-intentservice

5
推荐指数
1
解决办法
1724
查看次数

在Android中执行服务的最佳方法

我的服务具有可变的生命周期.它可以执行5分钟到2小时(例如).所以我正在寻找最好的方法,我的服务必须实现以下功能:

  • 每隔5秒发送(到我的服务器)lat-long和一些额外的信息(string's,boolean's和int's)

我尝试过"正常"服务并尝试做这样的事情来实现这个目的:

public class MyFiveSecondsService extends Service {
    private Handler handler;
    Runnable r = new Runnable() {
        @Override
        public void run() {
            //here send my new data
        }
    };

    public void onCreate(){
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(handler == null){
            handler = new Handler();
        }

        handler.post(r);

        return super.onStartCommand(intent, flags, startId);
    }
}
Run Code Online (Sandbox Code Playgroud)

实际上代码有效,但我遇到了一些性能问题,所以我试着这样做:

public class SendUniquePositionIntentService extends IntentService {

    public SendUniquePositionIntentService() {
        super("co.bomboapp.Service.IntentService.SendUniquePositionIntentService");
    }

    @Override
    public …
Run Code Online (Sandbox Code Playgroud)

android background-service android-service android-intentservice

5
推荐指数
1
解决办法
643
查看次数

未在前台IntentService中调用Android IntentService的onDestroy

我有一个IntentService

OnCreate我的呼唤中startForeground(1, buildUpdatingAssignmentsNotification());

@NonNull
private Notification buildUpdatingAssignmentsNotification() {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
        Bitmap icon = BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon);
        notificationBuilder.setSound(Uri.EMPTY)
                .setVibrate(new long[]{0})
                .setSmallIcon(R.drawable.app_icon)
                .setLargeIcon(icon)
                .setContentTitle(ASSIGNMENTS_GETTER_STARTED_MESSAGE)
                .setAutoCancel(true)
                .setProgress(0, 0, true);
        return notificationBuilder.build();
}
Run Code Online (Sandbox Code Playgroud)

最后,OnHandleIntent我调用了stopForeground(true);(由于看到通知消失,所以正在调用它),但是OnDestroy没有调用该方法。

如果我不使用前景的东西,它就可以正常工作,而且这个问题仅在我IntentService从活动开始启动并在IntentService完成之前关闭应用程序时发生(OnHandleIntent无论如何它都会完成,但不会调用)OnDestroy

任何想法如何解决这一问题?

android intentservice android-intentservice

5
推荐指数
0
解决办法
261
查看次数

登录屏幕显示progressdialog并允许更改屏幕方向

您好我正在尝试实现登录屏幕,显示进度对话框并允许手机旋转.

我想问一下最好的方法是什么(IntentService,AsyncTask,Service)并允许手机旋转?

我读了很多答案,说使用AsyncTask等空片段的不同内容.

android android-asynctask android-intentservice

5
推荐指数
1
解决办法
487
查看次数

地理围栏有效,但过了一段时间才停止触发

我的地理围栏正在开始工作,但是在一两天停止触发之后突然间,谷歌这边或我的代码有问题吗?

在启动并启动应用程序后,我使用IntentService然后注册Geofence:

public class RegisterGeoIntentService extends IntentService implements 
GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> {

private static final String TAG = "RegisterGeoIS";

private static final long TIME_OUT = 100;
protected GoogleApiClient mGoogleApiClient;
protected ArrayList<Geofence> mGeofenceList;
private PendingIntent mGeofencePendingIntent;

public RegisterGeoIntentService() {
    super(TAG);
}

@Override
public void onCreate() {
    super.onCreate();
    Log.i(TAG, "Creating Register Geo Intent service");
    mGeofenceList = new ArrayList<Geofence>();
    mGeofencePendingIntent = null;
}

@Override
protected void onHandleIntent(Intent intent) {
    buildGoogleApiClient();
    populateGeofenceList();
    mGoogleApiClient.blockingConnect(TIME_OUT, TimeUnit.MILLISECONDS);
    String connected = mGoogleApiClient.isConnected() ? "connected" : "disconnected";
    Log.i(TAG, "Restoring …
Run Code Online (Sandbox Code Playgroud)

performance android android-geofence android-intentservice android-doze

5
推荐指数
1
解决办法
629
查看次数

FirebaseMessagingService默认情况下是在后台运行吗?

FirebaseMessagingService在后台运行是否类似于IntentService运行方式?

我看到那些不在后台运行的FirebaseMessagingService扩展区Service,但是我想确定我是否应该在FirebaseMessagingService异步或同步中进行任何工作.

谢谢

android android-service firebase android-intentservice firebase-cloud-messaging

5
推荐指数
2
解决办法
3839
查看次数

IntentService + startForeground与JobIntentService

由于Android Oreo后台执行限制,因此文档建议将IntentServices 重构为JobIntentService

https://developer.android.com/about/versions/oreo/background

JobIntentService立即以IntentService低于Oreo的价格运行,但在Oreo +上安排作业

https://developer.android.com/reference/android/support/v4/app/JobIntentService

在什么情况下,将法线IntentService作为Service具有持久性通知的前台会有意义,什么时候JobIntentService更好?

我可以看到的一个缺点JobIntentService是它不会立即开始。

android android-service intentservice android-intentservice android-jobscheduler

5
推荐指数
1
解决办法
3129
查看次数