我正在创建一个应用程序将每天早上运行一次。我得到运行时异常错误,所以无法理解,如果应用程序真的在运行还是没有?
错误是:无法实例化接收器。即使我在我的清单上声明了接收者
主计划类.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);
}
}
爪哇
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>
最佳答案
您在receiver tag中写入了活动名称,将其更改为Tasks
即您的broadcastReceiver类。
替换为:
<receiver android:name="com.example.displayscheduler.MyScheduler" >
</receiver>
为此:
<receiver android:name="com.example.displayscheduler.Tasks" >
</receiver>
在你的
AndroidManifest.xml