当我想要创建一个帖子时,它只会在Android Marshmallow及其下方崩溃。我有一个自动启动脚本,它调用我的check.java脚本ON_BOOT_COMPLETED。在Android N上它可以工作,但它不会在Android Marshmallow上降低......它告诉我一个线程已经启动了,而那个线程是唯一被调用的线程...我试过像添加一样的东西睡眠(100);并检查线程是否存活,但没有任何效果......我该如何解决?
--------- beginning of crash
E/AndroidRuntime( 2436): FATAL EXCEPTION: main
E/AndroidRuntime( 2436): Process: com.company.app, PID: 2436
E/AndroidRuntime( 2436): java.lang.IllegalThreadStateException: Thread already started
E/AndroidRuntime( 2436): at java.lang.Thread.checkNotStarted(Thread.java:849)
E/AndroidRuntime( 2436): at java.lang.Thread.start(Thread.java:1059)
E/AndroidRuntime( 2436): at com.company.app.CheckRoosterWijziging$SayHello$1.run(CheckRoosterWijziging.java:94)
E/AndroidRuntime( 2436): at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime( 2436): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime( 2436): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime( 2436): at android.app.ActivityThread.main(ActivityThread.java:5254)
E/AndroidRuntime( 2436): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 2436): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 2436): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
E/AndroidRuntime( 2436): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我的主题:
Thread wijzigingenThread = new Thread() {
@Override
public void run() {
try {
sleep(100);
try {
System.out.println("Hello test");
} catch (Exception e) {
e.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Autostart.java
package com.company.app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class autostart extends BroadcastReceiver {
public void onReceive(Context arg0, Intent arg1) {
Intent intent = new Intent(arg0, CheckRoosterWijziging.class);
arg0.startService(intent);
}
}
CheckRoosterWijziging.java
package com.company.app;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.support.v4.app.NotificationCompat;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;
import java.util.regex.Pattern;
public class CheckRoosterWijziging extends Service {
private NotificationManager mNM;
private int NOTIFICATION = 10;
Handler handler;
MainActivity main = MainActivity.getMain();
final int NOTIFICATION_ID = 10;
NotificationManager notificationManager;
static CheckRoosterWijziging checkRoosterWijziging;
public class LocalBinder extends Binder {CheckRoosterWijziging getService() {return CheckRoosterWijziging.this;}}
@Override
public void onCreate() {
handler = new Handler(Looper.getMainLooper());
checkRoosterWijziging = this;
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
System.out.println("Service");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("CheckRoosterWijziging", "Received start id " + startId + ": " + intent);
showNotification();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
mNM.cancel(NOTIFICATION);
Toast.makeText(this, "Stopped", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent intent) {return mBinder;}
private final IBinder mBinder = new LocalBinder();
class SayHello extends TimerTask {
public void run() {
handler.post(new Runnable() {
public void run() {
wijzigingenThread.start();
}
});
}
}
private void showNotification() {
Timer timer = new Timer();
timer.schedule(new SayHello(), 15000, 15000);
}
答案 0 :(得分:1)
计时器在同一个start()
对象上多次调用Thread
,导致IllegalThreadStateException
。您可以在调用start之前使用指示线程是否已启动的标志,或检查Thread.getState()
。例如:
if (wijzigingenThread.getState().equals(Thread.State.NEW)) {
wijzigingenThread.start();
}
答案 1 :(得分:0)
通过将新的Thread()移动到调用原始wijzigingenThread.start()的位置来修复它。
所以我做了以下事情:
原件:
class SayHello extends TimerTask {
public void run() {
handler.post(new Runnable() {
public void run() {
wijzigingenThread.start();
}
});
}
}
新:
class SayHello extends TimerTask {
public void run() {
handler.post(new Runnable() {
public void run() {
new Thread() {
@Override
public void run() {
try {
sleep(100);
try {
System.out.println("Hello test");
} catch (Exception e) {
e.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
});
}
}
感谢@JoeC帮助我并指导我朝着正确的方向前进!