Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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_Kotlin_Alarmmanager_Android Alarms_Periodicity - Fatal编程技术网

如何在Android应用程序中每天重复一个动作?

如何在Android应用程序中每天重复一个动作?,android,kotlin,alarmmanager,android-alarms,periodicity,Android,Kotlin,Alarmmanager,Android Alarms,Periodicity,我想每天重复一个动作;即使应用程序未运行或设备已重新启动(重新启动),它也必须继续工作。 在我的代码中,我试图每1分钟显示一条TOAST消息(作为测试);它在模拟器中运行良好,但在实际设备上不起作用(我试图对fixed做一些更改,正如我在一些答案中看到的,但仍然是一样的) MyReceiver class MyReceiver : BroadcastReceiver() { private val channelId = "com.medanis.hikamwahimam" o

我想每天重复一个动作;即使应用程序未运行或设备已重新启动(重新启动),它也必须继续工作。 在我的代码中,我试图每1分钟显示一条TOAST消息(作为测试);它在模拟器中运行良好,但在实际设备上不起作用(我试图对fixed做一些更改,正如我在一些答案中看到的,但仍然是一样的)

MyReceiver

class MyReceiver : BroadcastReceiver() {
    private val channelId = "com.medanis.hikamwahimam"

    override fun onReceive(context: Context, intent: Intent) {
        Log.i("TAG","/////////////////// SHOW NOTIFICATION NOW //////////////////////")
        val builder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setLargeIcon(BitmapFactory.decodeResource(context.resources,R.mipmap.ic_launcher_round))
            .setContentTitle("My notification")
            .setContentText("Much longer text that cannot fit one line...")
            .setStyle(
                NotificationCompat.BigTextStyle()
                    .bigText("Much longer text that cannot fit one line...Much longer text that cannot fit one line..."))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        with(NotificationManagerCompat.from(context)) {
            notify(12345, builder.build()) }
        Toast.makeText(context,"This toast will be shown every X minutes", Toast.LENGTH_LONG).show()
    }
}
main活动

class MainActivity : AppCompatActivity() {
    private var mAlarmManager : AlarmManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
//        showNotification()

        val mIntent = Intent(this, MyReceiver::class.java)

        val mPendingIntent = PendingIntent.getBroadcast(this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT)
        mAlarmManager = this
            .getSystemService(Context.ALARM_SERVICE) as AlarmManager
        mAlarmManager!!.setRepeating(
            AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
            60000, mPendingIntent
        )
    }
}
AndroidManifest.xml

<receiver android:name=".MyReceiver" >
</receiver>

问题是:

1/-此代码不适用于实际设备

2/-如果用户重新启动其设备,则此代码不起作用

上的一个示例(我按照朋友的建议做了一些更改,但仍然存在相同的错误)

1/-此代码不适用于实际设备

我已经下载了你的项目,在我的设备上运行,它可以正常工作,当我单击开始时,它会显示
Toast
,并且每分钟都在显示

我建议你看看这个

2/-如果用户重新启动其设备,则此代码不起作用

如果要在设备重新启动或关闭后重新启动
BroadcastReceiver
,可能需要添加以下代码:

将此添加到您的
manifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
然后创建一个
BroadcastReceiver
,就像之前显示
Toast

class BootCompletedIntentReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if ("android.intent.action.BOOT_COMPLETED" == intent.action) {
          //Start your old broadcastreceiver
        }
    }
}
要了解更多信息,您可以看看这个


希望能有所帮助。

您能将您的修改添加到我的代码中并将其推送到github项目吗?据我所知,我对代码做了一些更改,但仍然存在相同的问题,请再次检查(我的设备API为24)有时设备块启动广播。进入设置->电池->你的应用程序,观察是否有toogle可以启用接收引导。按照Nikolay说的做@WIKOSOi有同样的问题@Skizo ozᴉ我尝试过所有的事情,但没有什么改变(第一次我在手机上试用应用程序)胡多尔M1“现在我已经尝试了“三星”,仍然是一样的事情)请回到我的代码在GITHUB上,检查它,并尝试与你的设备,你应该考虑使用WorkMealsRealm库作为。您可以在官方网站上了解更多信息。请在github中提供一个想法的示例!
class BootCompletedIntentReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        if ("android.intent.action.BOOT_COMPLETED" == intent.action) {
          //Start your old broadcastreceiver
        }
    }
}