在android中显示最近打开的应用程序的吐司

时间:2014-07-03 07:59:17

标签: java android performance android-activity

我正在尝试创建一个服务,该服务将使用最新运行的应用程序每秒显示一次吐司。每次启动服务时,都会出现NullPointerException。我该怎么做才能避免这种情况?

public class CheckRunningActivity extends Service {
private static final String TAG = "CheckRunningActivity";
boolean checkApps;
private Timer mTimer = null;
private Handler mHandler = new Handler();
private ActivityManager am;

public static final long NOTIFY_INTERVAL = 1000; // 1 second

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

@Override
public void onCreate() {
    Log.d(TAG, "I created it");   

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

     // cancel if already existed
    if(mTimer != null) {
        mTimer.cancel();
    } else {
        // recreate new
        mTimer = new Timer();
    }
    // schedule task
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}



class TimeDisplayTimerTask extends TimerTask {

    @Override
    public void run() {

        // run on another thread
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                Log.d(TAG, "Its running");   

        String packageName = am.getRunningTasks(1).get(0).topActivity
                .getPackageName();

            Toast.makeText(getBaseContext(), packageName, Toast.LENGTH_LONG).show();
            Log.d(TAG, "Make Toast");           
            }
        });
     }
 }          

}

2 个答案:

答案 0 :(得分:0)

你无法直接从服务中致电吐司。你需要一个处理程序。见答案here

答案 1 :(得分:0)

再次查看代码后,我意识到我没有正确初始化活动管理器。

这是更正后的代码......

public class CheckRunningActivity extends Service {
private static final String TAG = "CheckRunningActivity";
boolean checkApps;
private Timer mTimer = null;
private Handler mHandler = new Handler();
private ActivityManager am;

public static final long NOTIFY_INTERVAL = 1000; // 1 second

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

@Override
public void onCreate() {
    Log.d(TAG, "I created it");   


     // cancel if already existed
    if(mTimer != null) {
        mTimer.cancel();
    } else {
        // recreate new
        mTimer = new Timer();
    }
    // schedule task
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}



class TimeDisplayTimerTask extends TimerTask {

    @Override
    public void run() {

        // run on another thread
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
                Log.d(TAG, "Its running");   

        String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();

            Toast.makeText(getBaseContext(), packageName, Toast.LENGTH_LONG).show();
            Log.d(TAG, "Make Toast");           
            }
        });
     }
 }          
}