申请应该每天早上10点开始

时间:2014-07-21 12:03:20

标签: android alarmmanager android-pendingintent

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

错误是:无法实例化接收器。即使我在我的清单中宣布接收者

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();

            }
}

清单

<?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>

3 个答案:

答案 0 :(得分:4)

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

替换它:

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

到此:

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

AndroidManifest.xml

答案 1 :(得分:1)

MyScheduler类不是广播接收器,您必须使用manifest.xml中的任务更改它

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

答案 2 :(得分:1)

确保java文件名与接收者名称相同。 将您的java文件Tasks.java重命名为MyScheduler.java

参考此示例https://developer.android.com/training/scheduling/alarms.html