Héctor Sifuentes
Héctor Sifuentes

Reputation: 11

Service doesn't starts until exit and run again application - Android

Hello everyone I have a problem with an Android application that I'm developing! This app works as a remainder, It sends notification to user when an event occur, to decide when to send a notification, the app implements a IntentService, I want to send the notification from IntentService because IntentService works as a thread different from the UI thread or main thread, but when I install/run the app for first time after a Install it the service doesn't start and when I close the app and I run it again the service starts. I want to know why is this happening because if someone doesn't close the application after installing it Notifications just won't work and I can't tell users "Please close your app after you run it". I don't know If I'm implementing the service in a correct way. Could you please help me?

This is my Android Manifest:

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

<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".WelcomeActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Splashscreen"></activity>
    <activity android:name=".Registro" />
    <activity android:name=".Menuprincipal" />
    <activity android:name=".RecyclerViewActivity" />
    <activity android:name=".Evento" />
    <activity android:name=".Talleres" />
    <activity android:name=".Ponencias" />
    <activity android:name=".Favoritos" />
    <activity android:name=".DetalleEvento" />

    <receiver
        android:name=".NM_RemoteReceiver"
        android:process=":remote" >
    </receiver>

    <service
        android:name=".NM_RemoteService"
        android:exported="false" />

    <receiver android:enabled="true"
        android:name=".NM_BootReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

    <service android:name=".NM_BootService">
        <intent-filter>
            <action android:name="ServicioCodigo"></action>
        </intent-filter>
    </service>

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

As you can see the manifest allows the app to start when the device reboot or boot and also calls the service for Notifications.

Here is the classes where I implement the service:

This class is called by the manifest, from here I call the service that is located in another IntentService class:

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
import java.util.Calendar;

public class NM_RemoteReceiver extends BroadcastReceiver {



@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(context, NM_BootService.class);
    context.startService(i);

}
}

The IntentService class that is supossed to execute the service:

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

public class NM_BootService extends IntentService {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

public NM_BootService() {
    super("NM_RemoteService");
}
@Override
protected void onHandleIntent(Intent intent) { }

@Override
public void onCreate() {
    super.onCreate();
    //I implement notifications here
}
@Override
public void onDestroy() {
    super.onDestroy();
    //Print message when service destroys
}
}

I'm sorry for the text, my english isn't too good! :) If you can help me I'll apreciate it! Thank you!

Upvotes: 1

Views: 84

Answers (1)

Arun Shankar
Arun Shankar

Reputation: 2593

If you are implementing an Alarm (Reminder ) app, why are you just calling intent service?

Use AlarmManager (and use it wisely, since it eats up the battery)

https://developer.android.com/training/scheduling/alarms.html

Upvotes: 1

Related Questions