Android service START_STICKY not working in microm

2019-09-07 01:59发布

问题:

Android service START_STICKY not working in micromax q382 phone (android 5.1 lollipop) and working for Kitkat/marshmallow i.e working in lower version and higher version but not in specific to micromax q382 phone (android 5.1 lollipop).

Below is code for my service

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 java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Feroz on 11/01/2017.
 */

/*public class ServiceLoggerThread {
}*/


public class ServiceLoggerThread extends Service {
    private static long UPDATE_INTERVAL = 1 * 5 * 1000;  //default
    static Calendar calendar = Calendar.getInstance();
    static int count = 0;
    Context context;
    private static Timer timer = new Timer();

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        context = this;
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }

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

    }

    private void _startService() {
        timer.scheduleAtFixedRate(

                new TimerTask() {

                    public void run() {

                        doServiceWork();
                    }
                }, 1000, UPDATE_INTERVAL);
        Log.i(getClass().getSimpleName(), "FileScannerService Timer started....");
    }

    private void doServiceWork() {


        Log.e("Talentify", "callllllllllllllll" + calendar.getTime());
        //do something wotever you want
        //like reading file or getting data from network
        try {
            addNotification();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void _shutdownService() {
        if (timer != null) timer.cancel();
        Log.i(getClass().getSimpleName(), "Timer stopped...");
    }

    private void addNotification() {
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_action_action_search)
                        .setContentTitle("Notifications Example")
                        .setContentText("This is a test notification");

        Intent notificationIntent = new Intent(this, MainActivity.class);
        int id = count++;
        PendingIntent contentIntent = PendingIntent.getActivity(this, id, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        // Add as notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(id, builder.build());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        _shutdownService();

        // if (MAIN_ACTIVITY != null)  Log.d(getClass().getSimpleName(), "FileScannerService stopped");
    }

}

code for starting the service :

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

Manifest code for service

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

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

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

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

        <service
            android:name=".ServiceLoggerThread"
            android:enabled="true"
            />

    </application>

</manifest>

app level gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    packagingOptions {
        exclude 'LICENSE.txt'
    }
    defaultConfig {
        applicationId "com.mine"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    testCompile 'junit:junit:4.12'
    compile 'com.neovisionaries:nv-websocket-client:1.31'
    compile 'com.android.support:design:25.1.0'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.3.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.miguelcatalan:materialsearchview:1.4.0'

}