Java Android:在Asynctask中发送通知->;崩溃
我希望Java Android:在Asynctask中发送通知->;崩溃,java,android,android-asynctask,notifications,alarm,Java,Android,Android Asynctask,Notifications,Alarm,我希望扩展BroadcastReceiver的类首先使用jsoup阅读一个网站,然后(检查一些内容,然后)发送一个通知。 因为带有jsoup的部分需要一个AsyncTask,所以我猜通知代码需要在AsyncTask onPostExecute部分中,但是应用程序在报警或日历代码给出的时间崩溃 如果通知代码不在扩展AsyncTask的类中,则扩展的类中的广播接收器的代码将工作 这是目前NotificationClass的代码: public class NotificationClass exte
扩展BroadcastReceiver
的类首先使用jsoup阅读一个网站,然后(检查一些内容,然后)发送一个通知。
因为带有jsoup的部分需要一个AsyncTask
,所以我猜通知代码需要在AsyncTask onPostExecute
部分中,但是应用程序在报警
或日历
代码给出的时间崩溃
如果通知代码不在扩展AsyncTask
的类中,则扩展的类中的广播接收器
的代码将工作
这是目前NotificationClass的代码:
public class NotificationClass extends BroadcastReceiver{
Context specialContext;
int MID = 1;
long when = System.currentTimeMillis();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
new loadText().execute();
}
public class loadText extends AsyncTask<Void, Void, Void>
{
String siteText;
@Override
protected Void doInBackground(Void... params) {
Connection.Response res = null;
try {
res = Jsoup.connect("http://www.hpg-speyer.de/vertretungsplan/") // Link der Hauptseite, wo auch der Login ist
.data("authid", "22101999AzaKur", "authpw", "451d28") // Hier wären dann die Daten eingetragen
.method(Connection.Method.POST)
.execute();
} catch (Exception e) {
e.printStackTrace();
}
try {
Document doc = res.parse();
} catch (Exception e) {
e.printStackTrace();
}
String sessionId = res.cookie("PHPSESSID"); // Name des Cookies
Document doc2 = null;
try {
doc2 = Jsoup.connect("http://www.hpg-speyer.de/vertretungsplan/") // das wäre der Link zu den Feldern
.cookie("PHPSESSID", sessionId)
.get();
siteText = doc2.text();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
NotificationManager notificationManager = (NotificationManager) specialContext
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(specialContext, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(specialContext, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
specialContext).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Alarm Fired")
.setContentText("Events To be PErformed").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
if (siteText.contains("something")) {
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
}}
02-11 12:47:40.626 5413-5413/? I/art: Not late-enabling -Xcheck:jni (already on)
02-11 12:47:40.626 5413-5413/? W/art: Unexpected CPU variant for X86 using defaults: x86
02-11 12:47:40.708 5413-5413/de.kurt.vertretungsplan W/System: ClassLoader referenced unknown path: /data/app/de.kurt.vertretungsplan-1/lib/x86
02-11 12:47:40.711 5413-5413/de.kurt.vertretungsplan I/InstantRun: Instant Run Runtime started. Android package is de.kurt.vertretungsplan, real application class is null.
02-11 12:47:40.988 5413-5413/de.kurt.vertretungsplan W/System: ClassLoader referenced unknown path: /data/app/de.kurt.vertretungsplan-1/lib/x86
02-11 12:47:41.061 5413-5413/de.kurt.vertretungsplan W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-11 12:47:41.125 5413-5445/de.kurt.vertretungsplan D/NetworkSecurityConfig: No Network Security Config specified, using platform default
02-11 12:47:41.218 5413-5447/de.kurt.vertretungsplan I/OpenGLRenderer: Initialized EGL, version 1.4
02-11 12:47:41.218 5413-5447/de.kurt.vertretungsplan D/OpenGLRenderer: Swap behavior 1
02-11 12:47:41.248 5413-5447/de.kurt.vertretungsplan E/EGL_emulation: tid 5447: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
02-11 12:47:41.248 5413-5447/de.kurt.vertretungsplan W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9c73fe80, error=EGL_BAD_MATCH
我是如何注意到撞车的:
public class NotificationClass extends BroadcastReceiver{
Context specialContext;
int MID = 1;
long when = System.currentTimeMillis();
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
new loadText().execute();
}
public class loadText extends AsyncTask<Void, Void, Void>
{
String siteText;
@Override
protected Void doInBackground(Void... params) {
Connection.Response res = null;
try {
res = Jsoup.connect("http://www.hpg-speyer.de/vertretungsplan/") // Link der Hauptseite, wo auch der Login ist
.data("authid", "22101999AzaKur", "authpw", "451d28") // Hier wären dann die Daten eingetragen
.method(Connection.Method.POST)
.execute();
} catch (Exception e) {
e.printStackTrace();
}
try {
Document doc = res.parse();
} catch (Exception e) {
e.printStackTrace();
}
String sessionId = res.cookie("PHPSESSID"); // Name des Cookies
Document doc2 = null;
try {
doc2 = Jsoup.connect("http://www.hpg-speyer.de/vertretungsplan/") // das wäre der Link zu den Feldern
.cookie("PHPSESSID", sessionId)
.get();
siteText = doc2.text();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
NotificationManager notificationManager = (NotificationManager) specialContext
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(specialContext, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(specialContext, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
specialContext).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Alarm Fired")
.setContentText("Events To be PErformed").setSound(alarmSound)
.setAutoCancel(true).setWhen(when)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
if (siteText.contains("something")) {
notificationManager.notify(MID, mNotifyBuilder.build());
MID++;
}
}
}}
02-11 12:47:40.626 5413-5413/? I/art: Not late-enabling -Xcheck:jni (already on)
02-11 12:47:40.626 5413-5413/? W/art: Unexpected CPU variant for X86 using defaults: x86
02-11 12:47:40.708 5413-5413/de.kurt.vertretungsplan W/System: ClassLoader referenced unknown path: /data/app/de.kurt.vertretungsplan-1/lib/x86
02-11 12:47:40.711 5413-5413/de.kurt.vertretungsplan I/InstantRun: Instant Run Runtime started. Android package is de.kurt.vertretungsplan, real application class is null.
02-11 12:47:40.988 5413-5413/de.kurt.vertretungsplan W/System: ClassLoader referenced unknown path: /data/app/de.kurt.vertretungsplan-1/lib/x86
02-11 12:47:41.061 5413-5413/de.kurt.vertretungsplan W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-11 12:47:41.125 5413-5445/de.kurt.vertretungsplan D/NetworkSecurityConfig: No Network Security Config specified, using platform default
02-11 12:47:41.218 5413-5447/de.kurt.vertretungsplan I/OpenGLRenderer: Initialized EGL, version 1.4
02-11 12:47:41.218 5413-5447/de.kurt.vertretungsplan D/OpenGLRenderer: Swap behavior 1
02-11 12:47:41.248 5413-5447/de.kurt.vertretungsplan E/EGL_emulation: tid 5447: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
02-11 12:47:41.248 5413-5447/de.kurt.vertretungsplan W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9c73fe80, error=EGL_BAD_MATCH
我把日历的时间安排在相对较近的位置,然后运行应用程序,在虚拟设备上弹出并观察到后关闭它。
此方法在没有AsyncTask
和显示消息“HPG Vertretungsplan has stopped”(HPG Vertretungsplan has stopped)但有通知的情况下工作
提前感谢。特殊上下文变量为空。试试这个
SpecialContext = getActivity();
您的特殊上下文尚未分配。在onReceive中,将specialContext分配给传入上下文
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
specialContext=context;
new loadText().execute();}
把你的事故日志贴在这里,让我们看看日志。为什么要使用广播接收器?我想在这种情况下,“siteText”应该是空的,尽管崩溃日志有助于证实我的猜测。我编辑了这个问题:)谢谢;specialContext在loadText中使用,所以loadText是另一个类,所以是否仍然存在错误?我尝试了->在本例中,我没有看到任何外观