我想实现的事情:
FacebookServices
或WhatsApp
.我正在使用的物理设备的信息:
Android 10.
API Level 29
我尝试过的事情:
JobScheduler
WorkManager
Service (startService())
JobIntentService
AlarmManager
我面临的问题:
我读了很多文章,博客,看了很多视频,但还是没能达到我真正想要的目的。 我也知道,如果我使用带有通知通道的foregroundService,我的服务将不会被杀死和持久化,但我不希望这样,因为facebookServices肯定有办法在没有任何通知通道的情况下运行,它们只是在运行后台服务,甚至在API级别29上,并且在Android O之后有所有后台限制。
java.lang.IllegalStateException:不允许启动服务意图
.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.basicplayer">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyAlarmService"
android:enabled="true"
android:exported="true"
/>
<receiver android:name=".MyAlarmReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
MyAlarmReceiver.java
package com.example.basicplayer;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.provider.Settings;
import androidx.annotation.RequiresApi;
public class MyAlarmReceiver extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, MyAlarmService.class));
}
}
MyAlarmService.java
package com.example.basicplayer;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.Settings;
import androidx.annotation.Nullable;
public class MyAlarmService extends Service {
private MediaPlayer mediaPlayer;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer = MediaPlayer.create(this,
Settings.System.DEFAULT_ALARM_ALERT_URI);
mediaPlayer.setLooping(true);
mediaPlayer.start();
return START_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
try{
startService(new Intent(this, MyAlarmService.class));
}catch (Exception e){
e.printStackTrace();
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Mainactivity.java
package com.example.basicplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarm();
}
private void setAlarm() {
//getting the alarm manager
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//creating a new intent specifying the broadcast receiver
Intent i = new Intent(this, MyAlarmReceiver.class);
//creating a pending intent using the intent
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
//setting the repeating alarm that will be fired every day
am.setRepeating(AlarmManager.RTC, 10000, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
Toast.makeText(this, "Alarm is set", Toast.LENGTH_SHORT).show();
}
}
你们能帮忙吗?
在android的新版本中,对后台运行进程引入了限制。 他们这样做的原因是为了提供电池优化。 我认为您应该使用workmanager库。 这是一个很棒的后台处理库。 另外,当设备重新启动时,可以使用WorkManager启动作业。 我为你找到了一个答案:在android oreo中,设备重启后,PeriodicWorkRequest不工作