如何使用计划用于HTTP请求 [英] How to use Scheduler for Http Request

查看:134
本文介绍了如何使用计划用于HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Android的应用程序,一旦被安装在deive.It每5分钟发送一个HTTP rrequest。经过得到它表明我。如何能做到这一点通知的回应。

活动code

 公共类AutoNotification延伸活动{
    字符串URL =;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_auto_notification);
        postHtt prequest(测试,测试);    }    公共无效postHtt prequest(字符串userid,字符串传递){
        RequestClient reqClient =新RequestClient(AutoNotification.this);
        字符串ap presponse = NULL;
        尝试{
            URL =;
            Log.d(URL,网址);
            鸭presponse = reqClient.execute()获得()。
            字符串状态=200;
            Log.d(状态recived状态);            如果(status.equals(200)){
                autoNotify();
            }
        }赶上(例外五){
            Log.e(异常发生,异常是+ e.getMessage());
        }
    }
    公共无效autoNotify(){
        意向意图=新的Intent();
        的PendingIntent pIntent = PendingIntent.getActivity(AutoNotification.this,0,意向,0);
        建设者建设者=新NotificationCompat.Builder(getApplicationContext())
            .setTicker(测试题)。setContentTitle(内容测试)
            .setContentText(测试通知)。
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent);
        乌里notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(notificationSound);
        通知的NotI = builder.build();
        noti.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0,NotI位);    }
}

现在我怎么能调用这个每5分钟,显示notification.Please帮助我,这

修改
感谢@chintan的建议。我已经这样做了:

 公共类AutoNotification延伸活动{
    字符串URL =;
    私人计时器刷新= NULL;
    私人最终长refreshDelay = 5 * 1000;
    SendHtt prequestThread sendHtt prequestThread;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_auto_notification);        sendHtt prequestThread =新SendHtt prequestThread(测试,测试);
        sendHtt prequestThread.start();    }    公共无效postHtt prequest(字符串userid,字符串传递){
        RequestClient reqClient =新RequestClient(AutoNotification.this);
        字符串ap presponse = NULL;
        尝试{
            URL =;
            Log.d(URL,网址);
            鸭presponse = reqClient.execute(URL)获得();
            字符串状态=200;
            Log.d(状态recived状态);            如果(status.equals(200)){
                autoNotify();
            }
        }赶上(例外五){
            Log.e(异常发生,异常是+ e.getMessage());
        }
    }
    公共无效autoNotify(){
        意向意图=新的Intent();
        的PendingIntent pIntent = PendingIntent.getActivity(AutoNotification.this,0,意向,0);
        建设者建设者=新NotificationCompat.Builder(getApplicationContext())
            .setTicker(测试题)。setContentTitle(内容测试)
            .setContentText(测试通知)。
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent);
        乌里notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(notificationSound);
        通知的NotI = builder.build();
        noti.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0,NotI位);    }    类SendHtt prequestThread继承Thread {        布尔sendHtt prequest;
        字符串userid;
        字符串传递;        公共SendHtt prequestThread(字符串STR1,字符串STR2){
            this.userId = STR1;
            this.pass = STR2;
            sendHtt prequest = TRUE;
        }        公共无效stopSendingHtt prequest(){
            sendHtt prequest = FALSE;
        }        保护无效的onStop(){
            sendHtt prequestThread.stopSendingHtt prequest();
            super.stop();
        }        @覆盖
        公共无效的run(){
            而(sendHtt prequest){
                postHtt prequest(用户ID,通过);                SystemClock.sleep(refreshDelay);
            }
        }
    }
}


解决方案

我创建了一个自定义Thread类,这将睡5秒。

 公共类SendHtt prequestThread继承Thread {    布尔sendHtt prequest;
    字符串userid;
    字符串传递;    公共SendHtt prequestThread(字符串STR1,字符串STR2){
        this.userId = STR1;
        this.pass = STR2;
        sendHtt prequest = TRUE;
    }    公共无效stopSendingHtt prequest(){
        sendHtt prequest = FALSE;
    }    @覆盖
    公共无效的run(){
        而(sendHtt prequest){
            postRequest(用户ID,通过);
            //添加code在这里后台线程来执行
            autoNotify();
            SystemClock.sleep(delayMillis);
        }
    }
}

我没有实现的所有code,你需要把里面的,而循环。在这里, delayMillis 是整数值控股 5000 睡眠()需要的毫秒输入。

要执行此,写以下code。

  sendHtt prequestThread =新SendHtt prequestThread(测试,测试);
sendHtt prequestThread.start();

要制止这种,写以下code。

  sendHtt prequestThread.stopSendingHtt prequest();

如果你想停止这个线程时活动都已停止,然后写像下面。

  @覆盖
保护无效的onStop(){
    sendHtt prequestThread.stopSendingHtt prequest();
    super.onStop();
}

I want to create a application in Android that once it is installed in the deive.It send a HTTP rrequest in every 5 mins .After getting the response it shows the notification .How could i do this .

Activity Code

public class AutoNotification extends Activity {
    String url="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_notification);
        postHttpRequest("Test","Test");

    }

    public  void postHttpRequest(String userId,String pass){
        RequestClient reqClient = new RequestClient(AutoNotification.this);
        String AppResponse = null;
        try {
            url = "";
            Log.d("URL", url);
            AppResponse = reqClient.execute().get();
            String status = "200";
            Log.d("Status recived", status);

            if(status.equals("200")){
                autoNotify();
            }
        } catch (Exception e) {
            Log.e("Exception Occured", "Exception is "+e.getMessage());
        }
    }
    public void autoNotify(){
        Intent intent = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(AutoNotification.this, 0, intent, 0);
        Builder builder = new NotificationCompat.Builder(getApplicationContext())
            .setTicker("Test Title").setContentTitle("Content Test")
            .setContentText("Test Notification.")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent);
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(notificationSound);
        Notification noti = builder.build();    
        noti.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti);

    }
}

Now how could i call this on every 5 min and show the notification.Please help me this

Edit Thanks @chintan for Suggestion .i have done this :

public class AutoNotification extends Activity {
    String url="";
    private Timer refresh = null;
    private final long refreshDelay = 5 * 1000;
    SendHttpRequestThread sendHttpRequestThread; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_notification);

        sendHttpRequestThread = new SendHttpRequestThread("Test","Test");
        sendHttpRequestThread.start();

    }



    public  void postHttpRequest(String userId,String pass){
        RequestClient reqClient = new RequestClient(AutoNotification.this);
        String AppResponse = null;
        try {
            url = "";
            Log.d("URL", url);
            AppResponse = reqClient.execute(url).get();
            String status = "200";
            Log.d("Status recived", status);

            if(status.equals("200")){
                autoNotify();
            }
        } catch (Exception e) {
            Log.e("Exception Occured", "Exception is "+e.getMessage());
        }
    }
    public void autoNotify(){
        Intent intent = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(AutoNotification.this, 0, intent, 0);
        Builder builder = new NotificationCompat.Builder(getApplicationContext())
            .setTicker("Test Title").setContentTitle("Content Test")
            .setContentText("Test Notification.")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pIntent);
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(notificationSound);
        Notification noti = builder.build();    
        noti.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, noti);

    }

    class SendHttpRequestThread extends Thread {

        boolean sendHttpRequest;
        String userId;
        String pass;

        public SendHttpRequestThread(String str1, String str2) {
            this.userId = str1;
            this.pass = str2;
            sendHttpRequest = true;
        }

        public void stopSendingHttpRequest() {
            sendHttpRequest = false;
        }

        protected void onStop() {
            sendHttpRequestThread.stopSendingHttpRequest();
            super.stop();
        }

        @Override
        public void run() {
            while (sendHttpRequest) {
                postHttpRequest(userId, pass);

                SystemClock.sleep(refreshDelay);
            }
        }
    }
}

解决方案

I created one Custom Thread class which will sleep for 5 secs.

public class SendHttpRequestThread extends Thread {

    boolean sendHttpRequest;
    String userId;
    String pass;

    public SendHttpRequestThread(String str1, String str2) {
        this.userId = str1;
        this.pass = str2;
        sendHttpRequest = true;
    }

    public void stopSendingHttpRequest() {
        sendHttpRequest = false;
    }

    @Override
    public void run() {
        while (sendHttpRequest) {
            postRequest(userId, pass);
            //add code here to execute in background thread
            autoNotify();
            SystemClock.sleep(delayMillis);
        }
    }
}

I didn't implemented all code, you need to put inside while loop. Here delayMillis is integer value holding 5000 as sleep() takes input of mili seconds.

To execute this Thread, write following code.

sendHttpRequestThread = new SendHttpRequestThread("Test","Test");
sendHttpRequestThread.start();

To stop this Thread, write following code.

sendHttpRequestThread.stopSendingHttpRequest();

If you want to stop this thread when activity is stopped, then write like below.

@Override
protected void onStop() {
    sendHttpRequestThread.stopSendingHttpRequest();
    super.onStop();
}

这篇关于如何使用计划用于HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆