2

我制作了一个应用程序作为在后台运行的服务。这个应用程序基本上是一个电池警报。它工作正常,但唯一的问题是,当此服务运行时,它还会在活动应用程序任务管理器中显示此应用程序。因此,当我退出此应用程序时,它也会停止该服务。所以我想要的是仅当用户取消选中应用设置中的框时才停止此服务。如果它被选中,那么即使它在活动应用程序任务管理器中关闭,它也不应该被停止。

如何停止在任务管理器中显示我的应用程序?

我想我应该在这里提供代码这是我的服务类

public class BatteryService extends Service {
Notify notification = new Notify();
BatteryAlarm alarm = new BatteryAlarm();
private MediaPlayer mMediaPlayer;
boolean flag = false;

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

//method to start service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    notification.initNotification(this, false);
    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

//Broadcast receiver to get battery info
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context c, Intent i) {
        //notification.initNotification(c);

        int level = i.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int plugged = i.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

        SharedPreferences getAlarm = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String alarms = getAlarm.getString("ringtones", "content://media/internal/audio/media/45"); // /system/media/audio/ringtones/ANDROMEDA.ogg , content://media/internal/audio/media/45
        Uri uri = Uri.parse(alarms);

        if(plugged == 2) {
            if(level == 100) {
                if(uri != null) {
                    if(flag == false) {
                        playAlarm(c, uri);
                        notification.initNotification(c, true);
                        Toast.makeText(c, "Battery charge is completed. Unplug your mobile phone!", Toast.LENGTH_LONG).show();
                        flag = true;
                    }
                }
            }
        } else if (plugged == 0) {
            if(uri != null) {
                stopAlarm();
            }
            notification.cancelNotification(c);
            //Toast.makeText(c, "Mobile is unplugged", Toast.LENGTH_LONG).show();   
        }   
    }
};

//play alarm method
private void playAlarm(Context c, Uri uri) {

    mMediaPlayer = new MediaPlayer();
    try {
        mMediaPlayer.reset();
        mMediaPlayer.setDataSource(getBaseContext(), uri);
        final AudioManager audioManager = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        onDestroy();
    }

}

//method to stop playing alarm
private void stopAlarm() {
    mMediaPlayer.stop();
    flag = false;
}

//method to stop service
public void onDestroy() {
    super.onDestroy();
    notification.cancelNotification(this);
    unregisterReceiver(this.mBatInfoReceiver);
    stopAlarm();
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}

}

这是我的主要活动

public class BatteryNotify extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.xml.prefs);
    addPreferencesFromResource(R.xml.prefs);

    SharedPreferences getCB = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean cb = getCB.getBoolean("checkbox", true);
    final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkbox");

    if(cb == true) {
        startService(new Intent(getBaseContext(), BatteryService.class));
    } else if(cb == false) {
        stopService(new Intent(getBaseContext(), BatteryService.class));
    }

    checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if(newValue.toString().equals("true")) {
                startService(new Intent(getBaseContext(), BatteryService.class));
            } else {
                stopService(new Intent(getBaseContext(), BatteryService.class));
            }
            return true;
        }
    });

}

}

这是我的清单文件

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".BatteryNotify"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".BatteryService"></service>
</application>

4

2 回答 2

2

最好的方法是创建一个BroadcastReceiver,在清单中用适当intent-filter的 s 注册它,当它收到一个时,它会启动ServiceorActivity执行您需要的任何任务。

编辑:

创建BroadcastReceiver一个单独的类并将其注册到清单中。当它接收到电池事件时,创建一个PendingIntent来启动Service. 这样,如果您的应用程序未运行,则无关紧要。它将为您启动。

于 2012-05-13T11:14:44.663 回答
1

如何停止在任务管理器中显示我的应用程序?

出于明显的安全原因,您不能。

于 2012-05-13T11:10:29.500 回答