Java 如何在android中停止在活动应用程序窗口中显示应用程序
我制作了一个在后台运行的应用程序作为服务。这个应用程序基本上是一个电池报警器。它工作正常,但唯一的问题是,当此服务运行时,它还会在活动的应用程序任务管理器中显示此应用程序。因此,当我退出此应用程序时,它也会停止该服务。因此,我只想在用户取消勾选应用程序设置框时停止此服务。如果选中,则即使在活动应用程序任务管理器中关闭,也不应停止 如何停止在任务管理器中显示我的应用程序 我想我应该在这里提供代码这是我的服务类Java 如何在android中停止在活动应用程序窗口中显示应用程序,java,android,Java,Android,我制作了一个在后台运行的应用程序作为服务。这个应用程序基本上是一个电池报警器。它工作正常,但唯一的问题是,当此服务运行时,它还会在活动的应用程序任务管理器中显示此应用程序。因此,当我退出此应用程序时,它也会停止该服务。因此,我只想在用户取消勾选应用程序设置框时停止此服务。如果选中,则即使在活动应用程序任务管理器中关闭,也不应停止 如何停止在任务管理器中显示我的应用程序 我想我应该在这里提供代码这是我的服务类 public class BatteryService extends Service
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;
}
});
}
}
这是我的男朋友档案
如何停止在任务管理器中显示我的应用程序
出于明显的安全原因,您不能这样做。最好的方法是创建一个
BroadcastReceiver
,使用适当的intent过滤器将其注册到清单中,当它收到一个时,它启动服务
或活动
,以执行您需要的任何任务
编辑:
将您的BroadcastReceiver
创建为一个单独的类,并将其注册到清单中。当收到电池事件时,创建一个pendingent
,以启动服务。这样一来,你的应用程序是否没有运行就无关紧要了。它将为您启动。您是如何启动服务的?服务是否已在清单中注册?活动应该“仅仅”处理服务的控制(启动和停止),但不包含服务的代码。这是怎么回事:标记\u活动\u从\u最近事件中排除\u@techiServices我正在主活动中启动我的服务,基于复选框是否选中。@yoshi是的,服务在menifest文件中注册,但它仍在task manager.Commonware中显示此应用程序,那么gmail和其他一些应用程序是如何工作的?它们也不会显示在任务管理器中,但仍在后台运行。如果我不正确,请纠正我。@al0neevenings:这将是一个问题,无论是谁编写了您的“任务管理器”。只需说一句,如果你的应用程序中有任何部分正在运行,一个“任务管理器”就可以检测到并选择列出你。可能有上千个“任务管理器”应用程序可用,它们的工作方式都会有所不同。如果您从未面对过pendingent
(如我^,^)官方文档,那么下面的答案是很好的起点:
<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>