Monday, 11 March 2013

BackgroundUrlhit continously run in service

http://stackoverflow.com/questions/4443278/toast-sending-message-to-a-handler-on-a-dead-thread

AndroidNotifyService.java


import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class AndroidNotifyService extends Activity {
    /** Called when the activity is first created. */
    Intent intent;
    NotifyService notifyService;
    PendingIntent pintent;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button buttonStartService = (Button)findViewById(R.id.startservice);
        Button buttonStopService = (Button)findViewById(R.id.stopservice);
       
        buttonStartService.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
               
                /*intent = new Intent(AndroidNotifyService.this, NotifyService.class);
                startService(intent);
                */
               
                /*Calendar cur_cal = Calendar.getInstance();
                cur_cal.setTimeInMillis(System.currentTimeMillis());
                cur_cal.add(Calendar.SECOND, 50);
                Log.d("Testing", "Calender Set time:"+cur_cal.getTime());
               
                //Intent intent = new Intent(AndroidNotifyService.this, NotifyService.class);
                intent = new Intent(AndroidNotifyService.this, NotifyService.class);
                pintent = PendingIntent.getService(AndroidNotifyService.this, 0, intent, 0);
                AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(), 1*1000, pintent);
                */
               
                System.out.println("clicked");
                /*
                Intent i = new Intent ("com.example.serviceexample.BackgroundConnectionService");
                startService(i);*/
               
                Calendar cur_cal = Calendar.getInstance();
                cur_cal.setTimeInMillis(System.currentTimeMillis());
                cur_cal.add(Calendar.SECOND, 50);
                Log.d("Testing", "Calender Set time:"+cur_cal.getTime());
               
                //Intent intent = new Intent(AndroidNotifyService.this, NotifyService.class);
                intent = new Intent("com.example.serviceexample.BackgroundConnectionService");
                pintent = PendingIntent.getService(AndroidNotifyService.this, 0, intent, 0);
                AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(),1000, pintent);
               
               
               
            }});
       
        buttonStopService.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
               
                //stopService(intent);
                /*AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                alarmManager.cancel(pintent);
                */
            }});
       

    }
}



BackgroundConnectionService.java



package com.example.serviceexample;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.IntentService;
import android.content.Intent;


public class BackgroundConnectionService extends IntentService {

    String res="";
    public BackgroundConnectionService() {
        // Need this to name the service
        super ("ConnectionServices");
        System.out.println("t1");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
       
        BasicNameValuePair nameValuePair = new BasicNameValuePair("UserID", "v3390");
        List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
        nameValuePairList.add(nameValuePair);
       
        URLHit url = new URLHit("http://121.242.232.139:8082/Sales_Service/predocs", nameValuePairList);
        res = url.hitresponse();
        System.out.println("Response: "+res);
    }
}



AndroidManifest.xml


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.serviceexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".AndroidNotifyService"
            android:label="@string/title_activity_android_notify_service"
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
       
        <service android:name="com.example.serviceexample.BackgroundConnectionService">
            <intent-filter>
                <action android:name="com.example.serviceexample.BackgroundConnectionService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>
       

       
    </application>

</manifest>

No comments:

Post a Comment