2016-09-22 75 views
0

我想要使用AsyncTask在我的应用程序发送通知给我的用户时,在网站上,JSON值= \ =假。致电ASyncTask通知

的的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调用(为了在后台工作)。

谢谢

回答

1

AsynTask基本上可以让你在单独的线程(非UI是主线程)做操作的线程。 你不需要AsynTask的通知,可以使用的PendingIntent和听众像一个电话,触摸监听器来实现,等等

现在来到你的问题试试这个:

在你的主线程做以下:

ASynsTask myAsyncTask = new ASyncTask(getApplicationContext()); 
myAsyncTask.execute(); 

教程:Good read