问题描述
当我尝试从前台服务启动 Activity 并且应用程序未处于前台状态并从最近的项目中删除时,我的应用程序无一例外地停止工作。但在应用处于前台状态或打开主要活动时工作正常。
“绘制其他应用”权限也可从应用设置中获得。
我使用的是 oneplus 7 Pro,在 Android 11、API 30 上运行。
请帮帮我!!
ForgroundService.java
public int onStartCommand(Intent intent,int flags,int startId) {
Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
.setContentTitle("Content Title")
.setContentText("Content Text")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build();
startForeground(id,notification);
Intent activityIntent = new Intent(this,SecondActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(activityIntent);
return START_STICKY;
}
SecondActivity.java
@RequiresApi(api = Build.VERSION_CODES.O_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Window win = getwindow();
win.addFlags(WindowManager.LayoutParams.FLAG_disMISS_KEyguard);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setTurnScreenOn(true);
setShowWhenLocked(true);
setContentView(R.layout.second_activity);
}
second_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/textView"
android:layout_width="219dp"
android:layout_height="80dp"
android:text="SecondActivity"
android:textColor="#C50A0A"
android:textColorLink="#B11212"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.app">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SYstem_ALERT_WINDOW"/>
<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/Theme.BatteryApp">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.BatteryApp.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity">
</activity>
<service android:name=".ForgroundService" />
</application>
谢谢!!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)