Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法检测后台服务_Android_Android Service - Fatal编程技术网

Android 无法检测后台服务

Android 无法检测后台服务,android,android-service,Android,Android Service,我已经创建了一个应用程序,它增加数据库中存储的一个数字,并更新数据库中的数字。当应用程序被终止时,将发送一个广播,而接收器广播将执行与上述操作相同的操作。 我面临的问题是,当应用程序打开时,它会查看后台服务是否正在运行。但是当后台服务实际运行时,即使我为它编写了一个方法,它也无法检测。 我得到以下错误 2019-12-11 00:40:24.061 21588-21588/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.exa

我已经创建了一个应用程序,它增加数据库中存储的一个数字,并更新数据库中的数字。当应用程序被终止时,将发送一个广播,而接收器广播将执行与上述操作相同的操作。 我面临的问题是,当应用程序打开时,它会查看后台服务是否正在运行。但是当后台服务实际运行时,即使我为它编写了一个方法,它也无法检测。 我得到以下错误

2019-12-11 00:40:24.061 21588-21588/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 21588
    java.lang.RuntimeException: Unable to start receiver com.example.myapplication.SensorRestarterBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.myapplication/.MyService }: app is in background uid UidRecord{42ec1a7 u0a240 RCVR idle procs:1 seq(0,0,0)}
以下是我的主要活动和服务活动

Main Activity.java

public class MainActivity extends AppCompatActivity {

    TextView t1;

    DatabaseAccess databaseAccess;
    Intent mServiceIntent;
    private MyService mSensorService;
    Context ctx;
    public Context getCtx() {
        return ctx;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ctx = getApplicationContext();
        setContentView(R.layout.activity_main);

        int value = 0;

        databaseAccess=DatabaseAccess.getInstance(getApplicationContext());
        databaseAccess.open();
        value=databaseAccess.readValue();
        databaseAccess.close();

        t1=findViewById(R.id.text);
        t1.setText(""+value);

        mSensorService = new MyService(getCtx());
        mServiceIntent = new Intent(getCtx(), MyService.class);
        boolean stats=isMyServiceRunning(MyService.class);
        if (!stats) {
            startService(mServiceIntent);
        }
    }

    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(mServiceIntent);
    }
}

这回答了你的问题吗@greeble31但当我的应用程序被用户杀死时,我可以使用JobIntent运行服务吗?你可能想问一个单独的问题;我看不出这跟这个有什么关系。@greeble31我想和MyService类做同样的事情,但在JobIntent中,因为API级别在26之后,你必须使用JobScheduler在后台运行你的应用程序。我找不到任何东西可以帮助我在后台运行我的服务或应用程序,即使它被用户通过JobIntent杀死。然后再问一个单独的问题。你可以在这个网站上问多个问题。
public class MyService extends Service {
    public int inValue;
    DatabaseAccess databaseAccess;

    public MyService(Context applicationContext) {
        super();
    }

    public MyService() {
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
       //Read the database and increment the value in the counter which is available
        databaseAccess = DatabaseAccess.getInstance(getApplicationContext());
        databaseAccess.open();
        inValue = databaseAccess.readValue();
        databaseAccess.close();
        incremental(inValue);
       return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Intent broadcastIntent = new Intent(this,SensorRestarterBroadcastReceiver.class);
        sendBroadcast(broadcastIntent);
    }

    public void incremental(int i) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        i=i+1;
        databaseAccess=DatabaseAccess.getInstance(getApplicationContext());
        databaseAccess.open();
        databaseAccess.updateValue(i);
        databaseAccess.close();
    }
}