gpt4 book ai didi

android - 应用程序应该在每天早上 10 点运行

转载 作者:太空宇宙 更新时间:2023-11-03 12:34:10 26 4
gpt4 key购买 nike

我正在创建一个每天早上运行一次的应用程序。我收到 RunTimeException 错误,所以无法理解该应用程序是否真的在运行?

错误是:无法实例化接收器。即使我在 list 中声明了接收器

MainSchedulerClass.java

package com.example.displayscheduler;

import java.util.Calendar;
import java.util.TimeZone;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyScheduler extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule);

setRecurringAlarm(this);
}

private void setRecurringAlarm(Context context) {

Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getTimeZone("GMT+5:00"));
updateTime.set(Calendar.HOUR_OF_DAY, 10);
updateTime.set(Calendar.MINUTE, 00);

Intent intent = new Intent(context, Tasks.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) this.getSystemService(
Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, recurringDownload);
}
}

Tasks.java

package com.example.displayscheduler;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class Tasks extends BroadcastReceiver {

final public static String ONE_TIME = "onetime";

@Override
public void onReceive(Context context, Intent intent) {
Log.i(ONE_TIME, "Executed Tasks.Java File");
//Some task here for every morning
Toast.makeText(context, "Start Displaying Pictures", Toast.LENGTH_LONG).show();

}
}

list

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

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyScheduler"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Tasks"
android:enabled="true"
android:label="@string/app_name" >
</activity>

<receiver android:name="com.example.displayscheduler.MyScheduler" >
</receiver>
</application>

</manifest>

最佳答案

你在接收器标签中写了 Activity 名称,将其更改为 Tasks 即你的 BroadcastReceiver 类。

替换这个:

    <receiver android:name="com.example.displayscheduler.MyScheduler" >
</receiver>

为此:

    <receiver android:name="com.example.displayscheduler.Tasks" >
</receiver>

在你的AndroidManifest.xml

关于android - 应用程序应该在每天早上 10 点运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24864646/

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