Android MainActivity对象将一直保持到服务停止为止?
我希望启动一项服务,显示一个通知图标,并设置屏幕始终打开,然后单击通知图标关闭服务并关闭屏幕始终打开 因此,我在类MainActivity中创建(Bundle savedInstanceState)时在函数Android MainActivity对象将一直保持到服务停止为止?,android,android-service,Android,Android Service,我希望启动一项服务,显示一个通知图标,并设置屏幕始终打开,然后单击通知图标关闭服务并关闭屏幕始终打开 因此,我在类MainActivity中创建(Bundle savedInstanceState)时在函数中启动服务,但我认为系统将保留MainActivity的对象(我使用Intent Intent=new Intent(this,KeepOnService.class);),即使我执行finish(),对吗 顺便问一下,还有其他更好的方法来做这个应用吗?我是否可以使用静态变量来保持PowerM
中启动服务,但我认为系统将保留MainActivity的对象(我使用Intent Intent=new Intent(this,KeepOnService.class);
),即使我执行finish(),对吗
顺便问一下,还有其他更好的方法来做这个应用吗?我是否可以使用静态变量来保持PowerManager.WakeLock mWakeLock
和PowerManager pm代码>,但我认为静态变量将在活动关闭时释放
main活动
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (PublicFun.IsRunningAService(this, KeepOnService.class.getName() )== false) {
Intent intent = new Intent(this, KeepOnService.class);
startService(intent);
}
finish();
}
}
KeepOnService
public class KeepOnService extends Service {
private Context mContext;
PowerManager pm;
PowerManager.WakeLock mWakeLock;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
mContext=this;
pm = (PowerManager) mContext. getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag");
PublicFun.DisplayIconByContent(mContext);
ShowMessage("Created");
}
@Override
public void onDestroy(){
PublicFun.ClearIcon(mContext);
ShowMessage("Free");
mWakeLock.release();
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
boolean isStopService=intent.getBooleanExtra("IsStopService",false);
if (isStopService==true){
ShowMessage("Stop");
stopSelf();
}else {
ShowMessage("Start");
mWakeLock.acquire();
}
return super.onStartCommand(intent, flags, startId);
}
private void ShowMessage(String s){
Log.e("My", s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}
PublicFun
public class PublicFun {
public static void DisplayIconByContent(Context myContext) {
CharSequence contentTitle= myContext.getResources().getString(R.string.NotificationTitle);
String myContentText=myContext.getResources().getString(R.string.NotificationContent);
Intent notificationIntent = new Intent(myContext, KeepOnService.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("IsStopService",true);
PendingIntent contentItent = PendingIntent.getService(myContext, 0,notificationIntent, 0);
Notification.Builder builder = new Notification.Builder(myContext)
.setSmallIcon(R.drawable.newkeepon)
.setLargeIcon(BitmapFactory.decodeResource(myContext.getResources(), R.drawable.newkeepon))
.setContentTitle(contentTitle)
.setContentText(myContentText)
.setTicker(contentTitle)
.setContentIntent(contentItent);
Notification notification = builder.build();
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
NotificationManager notificationManager = (NotificationManager) myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
notificationManager.notify(myContext.getResources().getInteger(R.integer.NotificationID), notification);
}
public static void ClearIcon(Context myContext) {
NotificationManager notificationManager = (NotificationManager)myContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
notificationManager.cancel(myContext.getResources().getInteger(R.integer.NotificationID));
}
public static boolean IsRunningAService(Context mContext, String className){
ActivityManager myAM = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
ArrayList<ActivityManager.RunningServiceInfo> runningServices =
(ArrayList<ActivityManager.RunningServiceInfo>) myAM.getRunningServices(Integer.MAX_VALUE);
for (int i = 0; i < runningServices.size(); i++) {
if (runningServices.get(i).service.getClassName().toString().equals(className)) {
return true;
}
}
return false;
}
}
公共类公共娱乐{
公共静态void DisplayIconByContent(Context myContext){
CharSequence contentTitle=myContext.getResources().getString(R.string.NotificationTitle);
字符串myContentText=myContext.getResources().getString(R.String.NotificationContent);
Intent notificationIntent=newintent(myContext,KeepOnService.class);
notificationIntent.addFlags(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
notificationIntent.putExtra(“IsStopService”,true);
PendingContent contentient=PendingContent.getService(myContext,0,notificationIntent,0);
Notification.Builder=new Notification.Builder(myContext)
.setSmallIcon(R.drawable.newkeepon)
.setLargeIcon(BitmapFactory.decodeResource(myContext.getResources(),R.drawable.newkeepon))
.setContentTitle(contentTitle)
.setContentText(myContentText)
.setTicker(contentTitle)
.setContentIntent(contentintent);
通知通知=builder.build();
notification.flags |=notification.FLAG_持续事件;
notification.flags |=notification.FLAG_否_清除;
NotificationManager NotificationManager=(NotificationManager)myContext.getSystemService(android.content.Context.NOTIFICATION_服务);
notificationManager.notify(myContext.getResources().getInteger(R.integer.NotificationID),通知);
}
公共静态无效ClearIcon(上下文myContext){
NotificationManager NotificationManager=(NotificationManager)myContext.getSystemService(android.content.Context.NOTIFICATION_服务);
取消(myContext.getResources().getInteger(R.integer.NotificationID));
}
公共静态布尔ISRunningService(上下文mContext,字符串类名称){
ActivityManager myAM=(ActivityManager)mContext.getSystemService(Context.ACTIVITY_服务);
ArrayList运行服务=
(ArrayList)myAM.getRunningServices(Integer.MAX_值);
对于(int i=0;i
您可以试试这个,您能在开始服务(意图)之前尝试调用finish()
吗?谢谢当我在startService(intent)之前调用finish()时,应用程序可以正常运行。但是我认为finish()是异步的,当我在startService(intent)之前调用finish()时,可能会导致错误。你可以试试这个,你能在startService(intent)之前尝试调用finish()
吗?谢谢当我在startService(intent)之前调用finish()时,应用程序可以正常运行。但我认为finish()是异步的,当我在startService(intent)之前调用finish()时可能会导致错误