Java Android服务编辑

Java Android服务编辑,java,android,performance,android-fragments,Java,Android,Performance,Android Fragments,我使用eclipse制作了一个android应用程序,没有活动,只有一个类,其中的代码如下 public class PersistService extends Service { private static final int INTERVAL = 3000; // poll every 3 secs private static final string YOUR_APP_PACKAGE_NAME = "YOUR_APP_PACKAGE_NAME"; private static b

我使用eclipse制作了一个android应用程序,没有活动,只有一个类,其中的代码如下

public class PersistService extends Service {

private static final int INTERVAL = 3000; // poll every 3 secs
private static final string YOUR_APP_PACKAGE_NAME = "YOUR_APP_PACKAGE_NAME";

private static boolean stopTask;
private PowerManager.WakeLock mWakeLock;

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

    stopTask = false;

    // Optional: Screen Always On Mode!
    // Screen will never switch off this way
    mWakeLock = null;
    if (settings.pmode_scrn_on){
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "a_tag");
        mWakeLock.acquire();
    }

    // Start your (polling) task
    TimerTask task = new TimerTask() {
        @Override
        public void run() {

            // If you wish to stop the task/polling
            if (stopTask){
                this.cancel();
            }

            // The first in the list of RunningTasks is always the foreground task.
            RunningTaskInfo foregroundTaskInfo = activityManager.getRunningTasks(1).get(0);
            String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();

            // Check foreground app: If it is not in the foreground... bring it!
            if (!foregroundTaskPackageName.equals(YOUR_APP_PACKAGE_NAME)){
                Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(YOUR_APP_PACKAGE_NAME);
                startActivity(LaunchIntent);
            }
        }
    };
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(task, 0, INTERVAL);
}

@Override
public void onDestroy(){
    stopTask = true;
    if (mWakeLock != null)
        mWakeLock.release();
    super.onDestroy();
}
}

我通过以下方式注册我的服务`注册您的服务:

<service android:name="YOURPACAKGE.PersistService" 
 android:enabled="true"/>`

您需要将导入添加到此类

import android.app.*;
import android.content.*;
import android.os.*;
import java.util.*;

public class PersistService extends Service {

    //Class Content

}

注意:我不使用eclipse,但我认为它缺少一些插件或类似的东西来自动导入。Eclipse用户如果我错了,请纠正我。

您可以检查下面的代码,您没有导入任何存在问题的类,我不理解您的解决方案,为什么在应用程序打开时要打开包。。?我们也可以使用ActivityLifecycle来确定我们的应用程序是否正在打开

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.annotation.Nullable;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by muthukrishnan on 10/06/17.
 */

public class PersistService extends Service {

    private static final int INTERVAL = 3000; // poll every 3 secs
    private static final String YOUR_APP_PACKAGE_NAME = "YOUR_APP_PACKAGE_NAME";

    private static boolean stopTask;
    private PowerManager.WakeLock mWakeLock;

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

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

        stopTask = false;

        // Optional: Screen Always On Mode!
        // Screen will never switch off this way
        mWakeLock = null;

        // NOTE : @Muthu I am not sure what is this variable called settings
        if (settings.pmode_scrn_on){
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "a_tag");
            mWakeLock.acquire();
        }

        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        // Start your (polling) task
        TimerTask task = new TimerTask() {
            @Override
            public void run() {

                // If you wish to stop the task/polling
                if (stopTask){
                    this.cancel();
                }


                // The first in the list of RunningTasks is always the foreground task.
                ActivityManager.RunningTaskInfo foregroundTaskInfo = activityManager.getRunningTasks(1).get(0);
                String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();

                // Check foreground app: If it is not in the foreground... bring it!
                if (!foregroundTaskPackageName.equals(YOUR_APP_PACKAGE_NAME)){
                    Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(YOUR_APP_PACKAGE_NAME);
                    startActivity(LaunchIntent);
                }
            }
        };
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task, 0, INTERVAL);
    }

    @Override
    public void onDestroy(){
        stopTask = true;
        if (mWakeLock != null)
            mWakeLock.release();
        super.onDestroy();
    }
}

@tanasis您是否可以检查此PersistService:类型PersistService必须实现继承的抽象方法服务。onBind(Intent)还可以“private static final string YOUR\u APP\u PACKAGE\u NAME=“YOUR\u APP\u PACKAGE\u NAME”字符串具有erorr字符串无法解析为类型将activityManager更改为activityManager工作精细,activityManager.RunningTaskInfo foregroundTaskInfo=activityManager.getRunningTasks(1).get(0);get running task不推荐如何在AndroidMainFist中注册此服务只需将android target更改为4即可,如何在MainFist中注册此服务这是使此apk正常工作所需的权限,特别是当手机通电错误无法解决settings.pmode_scrn_on中的“设置”时。
import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;
import android.support.annotation.Nullable;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by muthukrishnan on 10/06/17.
 */

public class PersistService extends Service {

    private static final int INTERVAL = 3000; // poll every 3 secs
    private static final String YOUR_APP_PACKAGE_NAME = "YOUR_APP_PACKAGE_NAME";

    private static boolean stopTask;
    private PowerManager.WakeLock mWakeLock;

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

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

        stopTask = false;

        // Optional: Screen Always On Mode!
        // Screen will never switch off this way
        mWakeLock = null;

        // NOTE : @Muthu I am not sure what is this variable called settings
        if (settings.pmode_scrn_on){
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "a_tag");
            mWakeLock.acquire();
        }

        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        // Start your (polling) task
        TimerTask task = new TimerTask() {
            @Override
            public void run() {

                // If you wish to stop the task/polling
                if (stopTask){
                    this.cancel();
                }


                // The first in the list of RunningTasks is always the foreground task.
                ActivityManager.RunningTaskInfo foregroundTaskInfo = activityManager.getRunningTasks(1).get(0);
                String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();

                // Check foreground app: If it is not in the foreground... bring it!
                if (!foregroundTaskPackageName.equals(YOUR_APP_PACKAGE_NAME)){
                    Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(YOUR_APP_PACKAGE_NAME);
                    startActivity(LaunchIntent);
                }
            }
        };
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(task, 0, INTERVAL);
    }

    @Override
    public void onDestroy(){
        stopTask = true;
        if (mWakeLock != null)
            mWakeLock.release();
        super.onDestroy();
    }
}