【问题标题】:How to track user in android Oreo and above version如何在 android Oreo 及以上版本中跟踪用户
【发布时间】:2019-04-23 06:15:08
【问题描述】:

我想在 android Oreo 版本中每 30 分钟跟踪一次用户。当用户从最近的应用程序托盘中杀死时,跟踪服务将停止。我尝试使用 Foreground Service/Work Manager,它在 Pie 中运行良好,但对 Oreo 却不起作用。

意图 serviceIntent = new Intent(this, ExampleService.class); serviceIntent.putExtra("inputExtra", input); ContextCompat.startForegroundService(this,serviceIntent);

ExampleService.Java

public class ExampleService extends Service {

    private Timer mTimer;
    private Handler mHandler = new Handler();

    private static final int TIMER_INTERVAL = 120000; // 2 Minute
    private static final int TIMER_DELAY = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        if (mTimer != null)
            mTimer = null;

        mTimer = new Timer();
        mTimer.scheduleAtFixedRate(new DisplayToastTimerTask(), TIMER_DELAY, TIMER_INTERVAL);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input=intent.getStringExtra("inputExtra");

        Intent notificationIntent=new Intent(this, MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,
                notificationIntent,0);
        Notification notification=new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Example Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_android)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(1, notification);
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        startAgainService();
        super.onDestroy();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        startAgainService();
        super.onTaskRemoved(rootIntent);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private class DisplayToastTimerTask extends TimerTask {
        Handler mHandler = new Handler();
        @Override
        public void run() {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Hello world every 5 minute", Toast.LENGTH_SHORT).show();
                }
            });

        }
    }

    private void startAgainService() {
        Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
        restartServiceIntent.setPackage(getPackageName());

        PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarmService.set(
                AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 1000,
                restartServicePendingIntent);           
    }
}

【问题讨论】:

  • 设备的制造商是谁?
  • 一加、小米等
  • 这是添加自定义 android 的制造商的问题,如果您尝试在库存 android 上运行它,这不会发生,不幸的是我认为没有任何解决方案(我有同样的问题并花了几天时间寻找东西)

标签: android service android-workmanager background-foreground


【解决方案1】:

一些设备制造商已决定修改现有的 Android,以在用户从最近的应用程序中滑出应用程序时强制停止该应用程序。

正如 WorkManager 的公共 issuetracker - are the Chinese manufacturers (Huawei, Oppo, Xiaomi...) supported? 所报告的那样,没有办法解决这个问题。

在这些情况下,您的工作将停止,直到用户再次启动您的应用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多