我希望在我的应用上使用 AsyncTask ,以便在网站上向我的用户发送通知时,JSON值= \ = false。
AsyncTask如下:
public class ASyncTask extends AsyncTask<String, Void, String> {
private static boolean onair;
public static final String TAG = "[NOTIF]";
private final static int INTERVAL = 1000; //2 minutes
Handler mHandler = new Handler();
private final HttpClient httpclient = new DefaultHttpClient();
final HttpParams params = httpclient.getParams();
HttpResponse response;
private String content = null;
private boolean error = false;
private Context mContext;
private int NOTIFICATION_ID = 1;
private Notification mNotification;
private NotificationManager mNotificationManager;
Runnable mHandlerTask = new Runnable() {
@Override
public void run() {
detectonline_prepare();
mHandler.postDelayed(mHandlerTask, INTERVAL);
}
};
public ASyncTask(Context context){
this.mContext = context;
//Get the notification manager
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
}
protected void onPreExecute() {
Log.i(TAG, "Erreur onPretExecute");
}
protected String doInBackground(String... urls) {
detectonline_prepare();
return content;
}
protected void onCancelled() {
createNotification("Une erreur s'est produite",content);
}
protected void onPostExecute(String content) {
detectonline_prepare();
if (error) {
Log.i(TAG, "Erreur onPostExecute");
} else {
Log.i(TAG, "[NOTIF] OK");
createNotification("ok","ok");
}
}
private void createNotification(String contentTitle, String contentText) {
//Build the notification using Notification.Builder
Notification.Builder builder = new Notification.Builder(mContext)
.setSmallIcon(android.R.drawable.stat_sys_download)
.setAutoCancel(true)
.setContentTitle(contentTitle)
.setContentText(contentText);
//Get current notification
mNotification = builder.getNotification();
//Show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
public void detectonline_prepare(){
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
detectonline();
} catch (JSONException | IOException e) {
e.printStackTrace(System.out);
}
}
public void detectonline() throws JSONException, IOException {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
checkIfOnline();
if (onair == false) {
Log.i(TAG, "Variable OFF");
} else {
//createNotification();
Log.i(TAG, "Variable ON");
}
}
public static void checkIfOnline() throws JSONException, IOException {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String channerUrl = "https://api.dailymotion.com/video/x3p6d9r?fields=onair";
String jsonText = readFromUrl(channerUrl);// reads text from URL
JSONObject json = new JSONObject(jsonText); // You create a json object from your string jsonText
if (json.has("onair")) { // just a simple test to check the node that you need existe
//boolean onair = json.getBoolean("onair"); // In the url that you gave onair value is boolean type
onair = json.getBoolean("onair");
return;
}
}
private static String readFromUrl(String url) throws IOException {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
URL page = new URL(url);
StringBuilder sb = new StringBuilder();
Scanner scanner = null;
try {
scanner = new Scanner(page.openStream());
while (scanner.hasNextLine()) {
sb.append(scanner.nextLine());
}
} finally {
if (scanner != null)
scanner.close();
}
return sb.toString();
}
但我想知道这是如何工作的。如何从我的 MainActivity 调用此AsyncTask(以便在后台工作)。
谢谢
答案 0 :(得分:1)
AsynTask基本上是线程,允许您在单独的线程(非UI,即主线程)中执行操作。 您不需要AsynTask进行通知,可以使用PendingIntent和Listeners(如电话,触摸监听器等)来实现。
现在回答你的问题,试试这个:
在主线程中执行以下操作:
ASynsTask myAsyncTask = new ASyncTask(getApplicationContext());
myAsyncTask.execute();
教程:Good read