gpt4 book ai didi

java - 安排 alarmmanager 无法重启我的手机

转载 作者:行者123 更新时间:2023-11-30 00:01:50 30 4
gpt4 key购买 nike

在我没有重启我的手机之前一直在 toast ,但是在重启之后 broadcastreceiver2 没有收到并且没有任何反应。

我关注了http://stacktips.com/tutorials/android/how-to-start-an-application-at-device-bootup-in-android和许多其他但没有任何反应。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.k2.alarmmanagerdemo">
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.k2.alarmmanagerdemo.MyBroadcastReceiver"
android:enabled="true">
</receiver>
<receiver android:name="com.k2.alarmmanagerdemo.BroadCastRecevier2"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>

</application>

</manifest>

MainActivity.java

public class MainActivity extends Activity {
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button) findViewById(R.id.button1);

b1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startAlert();
}
});
}


public void startAlert() {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),
1000 * 5,pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();
}
}

MyBroadcastReceiver.java

public class MyBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

Toast.makeText(context, "Alarm...." + System.currentTimeMillis(), Toast.LENGTH_SHORT).show();
Log.i("Alarm.", "alarm called" + System.currentTimeMillis());

}
}

BroadCastRecevier2.java

public class BroadCastRecevier2 extends BroadcastReceiver {

MainActivity activity = new MainActivity();
@Override
public void onReceive(Context context, Intent intent) {


activity.startAlert();
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Intent i = new Intent();
i.setClassName("com.k2.alarmmanagerdemo",
"com.k2.alarmmanagerdemo.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Toast.makeText(context, "BOOT", Toast.LENGTH_SHORT).show();
Log.i("myboot","boot compleated inside");

}
}
}

最佳答案

从您的 <receiver> 中删除这一行BroadcastReceiver2的声明:

android:permission="android.permission.RECEIVE_BOOT_COMPLETED"

<receiver> 中的那一行标记告诉系统只有具有此权限的包才允许向您的类(class)发送 Intent .它可能会混淆系统并阻止 Intent从被发送到您的接收者。

您还可以找到 this article有助于了解警报和启动完成接收器。

关于java - 安排 alarmmanager 无法重启我的手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49792821/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com