Search code examples
androidandroid-intentandroid-serviceandroid-service-binding

unfortunately app has stopped while using service in thread


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 ?


Solution

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

    runOnUiThread(new Runnable() {
    
                            @Override
                            public void run() {
                               //yourtoast;
                            }
                        });