Android服务编辑

时间:2017-06-09 23:26:14

标签: 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 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"/>`

我有一些错误&#34;红线显示在&#34;。

line number 1      -> service  "cannot be resolved to a type"
line number 7      ->  power manager "cannot be resolved to a type"
line number 10    -> onCreat()     "The method onCreate() of typePersistService must override or implement a supertype method"

line number 11     -> super "Service cannot be resolved to a type"
line number 12     -> TimerTask "cannot be resolved to a type"
line number 28     -> activityManager "cannot be resolved"
line number 43     -> onDestroy()    "The method onDestroy() of type PersistService must override or implement a supertype method"

2 个答案:

答案 0 :(得分:1)

您需要向此类添加导入

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

public class PersistService extends Service {

    //Class Content

}

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

答案 1 :(得分:0)

你可以检查下面的代码,你没有导入任何问题的类,我不明白你的解决方案,为什么你应该在你的应用程序打开时打开你的包..?我们也可以使用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();
    }
}