1

I hope to start a service, show a notification icon, and set screen always on, and click the notification icon to close the service and turn off screen always on.

So I start the service in the function on Create(Bundle savedInstanceState) in the class MainActivity, but I think the system will hold the object of MainActivity (I begin the service using Intent intent = new Intent(this, KeepOnService.class); ) even if I execute finish(), right?

BTW, is there other the better way to do the App ? can I use static var to hold PowerManager.WakeLock mWakeLock and PowerManager pm;, but I think the static var will be released when a Activity closed.

MainActivity

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (PublicFun.IsRunningAService(this, KeepOnService.class.getName() )== false) {
            Intent intent = new Intent(this, KeepOnService.class);
            startService(intent);
        }

        finish();
    }

}

KeepOnService

public class KeepOnService  extends Service {

    private Context mContext;

    PowerManager pm;
    PowerManager.WakeLock mWakeLock;

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

    @Override
    public void onCreate(){
        mContext=this;
        pm = (PowerManager) mContext. getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");

        PublicFun.DisplayIconByContent(mContext);
        ShowMessage("Created");
    }

    @Override
    public void onDestroy(){
        PublicFun.ClearIcon(mContext);
        ShowMessage("Free");
        mWakeLock.release();
        super.onDestroy();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        boolean isStopService=intent.getBooleanExtra("IsStopService",false);

        if (isStopService==true){
            ShowMessage("Stop");
            stopSelf();
        }else {
            ShowMessage("Start");
            mWakeLock.acquire();
        }

        return super.onStartCommand(intent, flags, startId);
    }


    private void ShowMessage(String s){
        Log.e("My", s);
        Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
    }


}

PublicFun

public class PublicFun {

    public static void DisplayIconByContent(Context myContext) {

        CharSequence contentTitle= myContext.getResources().getString(R.string.NotificationTitle);
        String myContentText=myContext.getResources().getString(R.string.NotificationContent);

        Intent notificationIntent = new Intent(myContext, KeepOnService.class);
        notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        notificationIntent.putExtra("IsStopService",true);

        PendingIntent contentItent = PendingIntent.getService(myContext, 0,notificationIntent, 0);

        Notification.Builder builder = new Notification.Builder(myContext)
                .setSmallIcon(R.drawable.newkeepon)
                .setLargeIcon(BitmapFactory.decodeResource(myContext.getResources(), R.drawable.newkeepon))
                .setContentTitle(contentTitle)
                .setContentText(myContentText)
                .setTicker(contentTitle)
                .setContentIntent(contentItent);
        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_ONGOING_EVENT; 
        notification.flags |= Notification.FLAG_NO_CLEAR; 

        NotificationManager notificationManager = (NotificationManager) myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        notificationManager.notify(myContext.getResources().getInteger(R.integer.NotificationID), notification);
    }


    public static void ClearIcon(Context myContext) {
        NotificationManager notificationManager = (NotificationManager)myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(myContext.getResources().getInteger(R.integer.NotificationID));
    }



    public static boolean IsRunningAService(Context mContext, String className){
        ActivityManager myAM = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);

        ArrayList<ActivityManager.RunningServiceInfo> runningServices =
                (ArrayList<ActivityManager.RunningServiceInfo>) myAM.getRunningServices(Integer.MAX_VALUE);

        for (int i = 0; i < runningServices.size(); i++) {
            if (runningServices.get(i).service.getClassName().toString().equals(className)) {
                return true;
            }
        }
        return false;
    }


}
HelloCW
  • 843
  • 22
  • 125
  • 310
  • you can try this http://stackoverflow.com/questions/24307412/android-application-as-a-service-without-activity – koutuk Dec 21 '15 at 07:21
  • Can you try to call `finish()` before startService(intent); ? – pRaNaY Dec 21 '15 at 07:30
  • Thanks! The app can run correctly when I call finish() before startService(intent). But I think finish() is asynchronization, maybe it will cause error when I call finish() before startService(intent) – HelloCW Dec 21 '15 at 08:03

0 Answers0