私はシャットダウン後にデバイスが最初に起動したときに呼び出されるBootupという名前のブロードキャストレシーバ拡張クラスを持っています。私はこれを私の他のアプリの1つに実装しました。そのため、このコードをこのアプリケーションに移行し、必要なすべての詳細を変更しました。いいえ起動した受信者からのLogcatの印刷または通知
これは、それがマニフェストXMLファイル内のアプリケーションタグの下に登録されている方法です。
<receiver android:enabled="true" android:name=".reveivers.Bootup"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
そして、これはクラス自体です:
public class Bootup extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Received when the phone boots up
//Do some stuff
//Test notification to check if it worked. Doesn't show, but the method is valid code.
playNotification(context, "Active English", "Bootup received!");
//More code, then this bit to check if it works again
Log.d("test", milliseconds + " _ " + System.currentTimeMillis());
//Yet more code
}
private void playNotification(Context context, String name, String description){
long[] vibrate = new long[2];
vibrate[0] = 500;
vibrate[1] = 1000;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentText(description)
.setSmallIcon(R.drawable.notificationicon)
.setLights(Color.GREEN, 500, 2000)
.setAutoCancel(true)
.setVibrate(vibrate)
.setContentTitle(name);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
}
}
私は、今、すべてのあなたを教えてあげますコードは有効です。しかし何らかの理由で、logcatメッセージも通知も画面に表示されません。他のアプリケーションでもplayNotificationメソッドが正常に機能しているため、問題はありません。
ここに任意のアイデアはありますか?
EDIT:通知方法のコードが追加されました。
テストを行う前にアプリを少なくとも1回起動しましたか? –
はい、AndroidStudioからコードを実行した直後にアプリが起動しました。私はまた、アプリを開いた後、再起動して再確認しましたが、何も起こりません。 – NuffsaidM8