【问题标题】:My Android app is unable to run in the background我的 Android 应用无法在后台运行
【发布时间】:2021-09-18 08:59:52
【问题描述】:

我正在学习警报。我创建了一个应用程序,单击按钮会在 5 秒后向您发送通知。该应用程序在屏幕上运行良好。但是当我的应用程序在按下按钮后进入后台时它不起作用。应该来的通知,只有在我打开我的应用程序后才会出现

我尝试通过将设置中的“将未使用的应用程序置于睡眠状态”选项更改为 false 来更改三星手机的设置。但是还是不行。

我的代码有问题吗?还是我需要使用某种权限?

代码: activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set Alarm!"
        android:id="@+id/btn"
       />

</LinearLayout>

MainActivity.kt:

package com.example.alarms1

import android.app.*
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PowerManager
import android.os.SystemClock
import android.widget.Toast
import androidx.core.app.NotificationCompat
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            var packageName = this .getPackageName();
            var pm = getSystemService(POWER_SERVICE) as PowerManager;
            if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                var intent =Intent();
                intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setData(Uri.parse("package:" + packageName));
                startActivity(intent);
            }
        }
        Toast.makeText(this,"Hello",Toast.LENGTH_SHORT).show();
        btn.setOnClickListener {

            var int=Intent(this,MainActivity2::class.java)
            var pi=PendingIntent.getActivity(this,123,int,PendingIntent.FLAG_ONE_SHOT);
            var myAlarmm=getSystemService(ALARM_SERVICE) as AlarmManager;
            myAlarmm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime()+5000,pi);
        }
    }
}

activity_main2.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity2.kt:

package com.example.alarms1

import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.NotificationCompat

class MainActivity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        var nm=getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.O)
            nm.createNotificationChannel(
                NotificationChannel("Channel1","Name1",
                    NotificationManager.IMPORTANCE_DEFAULT)
            )

        var myNotification= NotificationCompat.Builder(this,"Channel1")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("Hello World")
            .setContentText("Hello namaste stasriaka").build()
        nm.notify(123,myNotification);

        finish();
    }
}

我还让我的 activity_main2 变得透明,效果很好

【问题讨论】:

标签: android android-studio kotlin android-permissions android-alarms


【解决方案1】:

三星非常积极地杀死在后台运行的东西。 dontkillmyapp.com/samsung 提供了您可能想要查看的内容的完整列表(取决于您的型号和操作系统版本),但简短列表如下:

  1. 禁用“使未使用的应用进入睡眠状态”
  2. 禁用“自动禁用未使用的应用”
  3. 从“睡眠”应用列表中删除您的应用
  4. 为您的应用禁用后台限制

【讨论】:

    【解决方案2】:

    您需要尝试使用 JobServices(https://developer.android.com/reference/android/app/job/JobService) 或/和 Worker(https://developer.android.com/topic/libraries/architecture/workmanager/advanced/worker) 取决于您的要求和 Android SDK。 我你还想用 AlarmManager 看看这个帖子:Alarm Manager does not work in background on Android 6.0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多