LoginSignup
0
0

More than 3 years have passed since last update.

HELP!!⇒動いたなう【Kotlin】 端末起動時にレシーバーで受けて処理が出来ない。。教えてください。

Last updated at Posted at 2020-11-19

かれこれ相当な時間を要したが、未だに解決出来ず助けて欲しいです。。。

やりたいこと: 端末起動時にアプリからローカル通知を設定し直す

Android端末は再起動すると、
セットしてたAlarmManagerの処理は揮発しちゃうんですよね。。

そこで下記記事を見ていろいろやっては見たのですが
Android の AlarmManager を改めて整理してみる
does Alarm Manager persist even after reboot?

アプリ起動時にレシーバーで受けるところがそもそも出来ていない様です
(レシーバーでトースト表示しにかかってますが一向に反応なし。。)

プロジェクト作りなおして、端末再起動時のレシーバーでの取得部分のみ最小限でやってみたのが下記です。

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <uses-permission android:name="android.permission.ACTION_BOOT_COMPLETED"/>

    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name=".BootCompletedReceiver"
            android:enabled="true"
            android:exported="false"
            android:process="">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
BootCompletedReceiver.kt
package com.example.myapplication

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast

class BootCompletedReceiver : BroadcastReceiver() {
    @SuppressLint("SimpleDateFormat")

    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == "android.intent.action.BOOT_COMPLETED") {
            // 確認用
            val toast: Toast = Toast.makeText(context, "yes", Toast.LENGTH_LONG)
            toast.show()

            Intent(context, MainActivity::class.java).apply {
                flags = Intent.FLAG_ACTIVITY_NEW_TASK
                context?.startActivity(this)
            }


        }
    }
}

これで実機で再起動したり、
こちらの記事再起動をadbコマンドで実行する
を参考にしてエミュレーターにたいして下記コマンド使ってトーストが出てくるのを期待するもいっこうに反応がありません。

# am broadcast -a android.intent.action.ACTION_BOOT_COMPLETED

結構時間費やしたのですが万作尽きた感じです。。情けない。
誰か助けて下さい。

以上、宜しくお願い致します。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0