0

I am new to android development, and trying to make demo application on service. But as tutorials describe that services are running on main UI thread, I have created thread and put my service in that thread to execute in background. and it is also working fine in background for few seconds and then application close by saying "unfortunately app has stopped".

Here is my code,

Service class

public class UpdateLocation extends Service {

    private class UpdateLocationThread implements Runnable{
        int service_id;
        UpdateLocationThread(int service_id){
            this.service_id = service_id;
        }

        @Override
        public void run() {
            int i= 0;
            synchronized (this){
                while (i <= 10){
                    try {
                        wait(15000);
                        i++;
                        Toast.makeText(UpdateLocation.this, "Service ends...", Toast.LENGTH_SHORT).show();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            stopSelf(service_id);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Toast.makeText(UpdateLocation.this, "Service ends...", Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(UpdateLocation.this, "Service started...", Toast.LENGTH_SHORT).show();
        Thread thread = new Thread(new UpdateLocationThread(startId));
        thread.start();

        return  START_STICKY;
    }

}

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.keval.mejodo" >

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:name=".Model.commonFuncs"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" > <!-- android:theme="@style/Theme.AppCompat.NoActionBar" -->

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity
            android:name=".MyAppBaseActivity"
            android:label="@string/title_activity_my_app_base" >
        </activity>

        <service android:name=".MServices.UpdateLocation"
            android:exported="false"></service>
    </application>

</manifest>

Calling service by doing,

Intent intent = new Intent(this, UpdateLocation.class);
            startService(intent);

What could be the reason for stop application ? How to fix it ?

Keval Patel
  • 925
  • 4
  • 24
  • 46
  • 1
    you can't show Toast from thread other than UI thread – Jaiprakash Soni Aug 21 '15 at 04:08
  • Yeah you are correct, As I removed Toast, its working fine, u can post it as answer. One more doubt, Can I make HTTP calls to my API's in Thread run method by passing my current location from front end? I want to make request in background only and after each specific period of time. – Keval Patel Aug 21 '15 at 04:32
  • Always make HTTP calls in separate thread – Jaiprakash Soni Aug 21 '15 at 04:53

2 Answers2

4

You can't show Toast on the UI thread, if it is necessary do it like

runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                           //yourtoast;
                        }
                    });
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
1

You are showing Toast inside thread which is only allowed in UI Thread, remove it or call it in UI Main Thread.

Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67