Android 为什么在几秒钟后强制关闭服务?
这是我应用程序中的服务类。 这是一个应用程序,显示两个图标(imageView)作为系统提醒窗口,这些图标可以打开两个活动。 启动服务时,在几分钟后或从服务执行活动后,然后在几秒钟后恢复,服务已被强制关闭两次Android 为什么在几秒钟后强制关闭服务?,android,android-service,Android,Android Service,这是我应用程序中的服务类。 这是一个应用程序,显示两个图标(imageView)作为系统提醒窗口,这些图标可以打开两个活动。 启动服务时,在几分钟后或从服务执行活动后,然后在几秒钟后恢复,服务已被强制关闭两次 package com.MDGH.Always_Accessible; import android.app.Service; import android.content.Context; import android.content.Intent; import android.co
package com.MDGH.Always_Accessible;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.graphics.PixelFormat;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class ServiceStart extends Service implements OnClickListener , OnTouchListener{
private static WindowManager wm;
private View topLeftView;
private int originalXpos, originalYpos, originalXpos2, originalYpos2;
private float offsetX, offsetY, offsetX2, offsetY2;
public static FrameLayout popup_show_overlay,sms_read_overlay;
public static ImageView popup_show_icon,sms_RW_icon;
private boolean moving, moving2;
public static boolean popup_show_Swap=false,sms_read_Swap=false, serviceButtonClicked=false;
private static FrameLayout subScript_overlay;
private static boolean trueTopFalseBottom=false;
private static TextView subScriptTextView;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
sms_read_overlay = (FrameLayout)inflater.inflate(R.layout.floating_sms_read, null);
popup_show_overlay = (FrameLayout)inflater.inflate(R.layout.floating_popup_show, null);
subScript_overlay = (FrameLayout)inflater.inflate(R.layout.floating_subscript, null);
subScriptTextView = (TextView)subScript_overlay.findViewById(R.id.subScriptTextView);
sms_RW_icon = (ImageView)sms_read_overlay.findViewById(R.id.sms_read);
popup_show_icon = (ImageView)popup_show_overlay.findViewById(R.id.popup_show);
if(sms_read_Swap==false)sms_RW_icon.setImageResource(R.drawable.sms_write);
else sms_RW_icon.setImageResource(R.drawable.sms_read);
ApplyAppearances();
sms_RW_icon.setVisibility(View.INVISIBLE);
sms_read_overlay.setVisibility(View.INVISIBLE);
subScript_overlay.setVisibility(View.INVISIBLE);
subScriptTextView.setVisibility(View.INVISIBLE);
popup_show_overlay.setOnTouchListener(this);
sms_read_overlay.setOnTouchListener(this);
popup_show_overlay.setOnClickListener(this);
sms_read_overlay.setOnClickListener(this);
subScript_overlay.setOnTouchListener(this);
subScript_overlay.setOnClickListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 0;
params.y = 0;
wm.addView(subScript_overlay, params);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 3000;
params.y = ConfigMain.popup_show_height+10;
wm.addView(sms_read_overlay, params);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = 3000;
params.y = 0;
wm.addView(popup_show_overlay, params);
topLeftView = new View(this);
WindowManager.LayoutParams topLeftparams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
topLeftparams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftparams.x = 0;
topLeftparams.y = 0;
topLeftparams.width = 0;
topLeftparams.height = 0;
wm.addView(topLeftView, topLeftparams);
sms_read_overlay.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(!moving2){
}
return false;
}
});
/*
popup_show_overlay.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(!moving){
}
return false;
}
});*/
}
@Override
public void onDestroy() {
super.onDestroy();
if(SmsBoxWrite.smsBoxWriteHasRuning)SmsBoxWrite.popUpWindowWrite.dismiss();
if(SmsBoxRead.smsBoxReadHasRuning)SmsBoxRead.popUpWindowRead.dismiss();
if(popup_show_overlay!=null){
wm.removeView(popup_show_overlay);
popup_show_overlay=null;
}
if(sms_read_overlay!=null){
wm.removeView(sms_read_overlay);
sms_read_overlay=null;
}
if(subScript_overlay!=null){
wm.removeView(subScript_overlay);
subScript_overlay=null;
}
wm.removeView(topLeftView);
topLeftView=null;
Editor editor = ConfigMain.sp.edit();
editor.putBoolean("isActive", false);
editor.commit();
Toast.makeText(getApplicationContext(), "Destroy",Toast.LENGTH_SHORT).show();
//popup_show_overlay.setVisibility(View.INVISIBLE);
}
long sdf1,sdf2;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.popup_show_frame:
if(event.getAction() == MotionEvent.ACTION_DOWN){
sdf1 = System.currentTimeMillis();
float x = event.getRawX();
float y = event.getRawY();
moving = false;
int[] location = new int[2];
popup_show_overlay.getLocationOnScreen(location);
originalXpos = location[0];
originalYpos = location[1];
offsetX = originalXpos -x ;
offsetY = originalYpos -y;
}
else if( event.getAction() == MotionEvent.ACTION_MOVE){
sdf2 = System.currentTimeMillis();
if((sdf2-sdf1)>2000)serviceExit();
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYpos);
float x = event.getRawX();
float y = event.getRawY();
WindowManager.LayoutParams params = (LayoutParams)popup_show_overlay.getLayoutParams();
int newX = (int) (offsetX + x);
int newY = (int) (offsetY + y);
if(Math.abs(newX -originalXpos)<1 && Math.abs(newY - originalYpos) <1 && !moving)return false;
params.x = newX - (topLeftLocationOnScreen[0]);
params.y = newY - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(popup_show_overlay, params);
moving =true;}
else if ( event.getAction() == MotionEvent.ACTION_UP){
boolean moving22 = moving;
moving=false;
if(moving22) return true;
}
return false;
case R.id.sms_read_frame:
if(event.getAction() == MotionEvent.ACTION_DOWN){
float x2 = event.getRawX();
float y2 = event.getRawY();
moving2 = false;
int[] location2 = new int[2];
sms_read_overlay.getLocationOnScreen(location2);
originalXpos2 = location2[0];
originalYpos2 = location2[1];
offsetX2 = originalXpos2 -x2 ;
offsetY2 = originalYpos2 -y2;
}
else if( event.getAction() == MotionEvent.ACTION_MOVE){
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYpos2);
float x2 = event.getRawX();
float y2 = event.getRawY();
WindowManager.LayoutParams params = (LayoutParams)sms_read_overlay.getLayoutParams();
int newX2 = (int) (offsetX2 + x2);
int newY2 = (int) (offsetY2 + y2);
if(Math.abs(newX2 -originalXpos2)<1 && Math.abs(newY2 - originalYpos2) <1 && !moving2) return false;
params.x = newX2 - (topLeftLocationOnScreen[0]);
params.y = newY2 - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(sms_read_overlay, params);
moving2 =true;}
else if ( event.getAction() == MotionEvent.ACTION_UP){
boolean moving22 = moving2;
moving2=false;
if(moving22) return true;
}
return false;
default:
return false;
}
}
private void serviceExit() {
Editor editor = ConfigMain.sp.edit();
editor.putBoolean("isActive", false);
editor.commit();
ScaleAnimation sc = new ScaleAnimation(1,0, 1, 0, ConfigMain.popup_show_width/2, ConfigMain.popup_show_height/2);
sc.setDuration((int)(300*ConfigMain.animationSpeed));
ServiceStart.popup_show_icon.startAnimation(sc);
sc.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0) {}
public void onAnimationRepeat(Animation arg0) {}
@Override
public void onAnimationEnd(Animation arg0) {
stopSelf();
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
}});
}
@Override
public void onClick(View v) {
if((moving2)||(moving))return;
switch (v.getId()) {
case R.id.popup_show_frame:
popup_show_Swap=!popup_show_Swap;
if(popup_show_Swap==true){
AlphaAnimation al = new AlphaAnimation(1, 4/10);
al.setDuration((int)(200*ConfigMain.animationSpeed));
popup_show_icon.startAnimation(al);
al.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0) {}
public void onAnimationRepeat(Animation arg0) {}
@Override
public void onAnimationEnd(Animation arg0) {
popup_show_icon.setImageResource(R.drawable.tick);
AlphaAnimation al = new AlphaAnimation(4/10, 1);
al.setDuration((int)(200*ConfigMain.animationSpeed));
popup_show_icon.startAnimation(al);
}
});
}
else{
AlphaAnimation al = new AlphaAnimation(1, 4/10);
al.setDuration((int)(400*ConfigMain.animationSpeed));
popup_show_icon.startAnimation(al);
al.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0) {}
public void onAnimationRepeat(Animation arg0) {}
@Override
public void onAnimationEnd(Animation arg0) {
popup_show_icon.setImageResource(R.drawable.not);
AlphaAnimation al = new AlphaAnimation(4/10, 1);
al.setDuration((int)(400*ConfigMain.animationSpeed));
popup_show_icon.startAnimation(al);
}});
}
serviceButtonClicked=true;
if(popup_show_Swap==true){
smsReadIconVisibleWithAnim();
if(sms_read_Swap==true){
SmsTextBoxReadVisible();
}
else if(sms_read_Swap==false){
SmsTextBoxWriteVisible();
}
}
else if(popup_show_Swap==false){
smsReadIconDisibleWithAnim();
if(SmsBoxWrite.smsBoxWriteHasRuning)SmsBoxWrite.popUpWindowWrite.dismiss();
if(SmsBoxRead.smsBoxReadHasRuning)SmsBoxRead.popUpWindowRead.dismiss();
sms_read_overlay.setVisibility(View.INVISIBLE);
sms_RW_icon.setVisibility(View.INVISIBLE);
//Toast.makeText(getApplicationContext(), "Default", Toast.LENGTH_SHORT).show();
}
break;
case R.id.sms_read_frame:
smsReadWriteChangeIconWithAnim1();
break;
case R.id.subScriptFrame:
trueTopFalseBottom=!trueTopFalseBottom;
int subScriptWidth = (int) ((subScriptTextView.getText().toString().length())*(ConfigMain.width/35));
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
params.width = (int) (subScriptWidth*1.5);
if(trueTopFalseBottom==true) params.gravity = Gravity.TOP;
if(trueTopFalseBottom==false) params.gravity = Gravity.BOTTOM;
params.x = 0;
params.y = 0;
wm.updateViewLayout(subScript_overlay, params);
break;
}
}
private void SmsTextBoxWriteVisible() {
Intent dialogIntent = new Intent(ServiceStart.this, SmsBoxWrite.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
getApplicationContext().startActivity(dialogIntent);
//Toast.makeText(getApplicationContext(), "WriteVisible", Toast.LENGTH_SHORT).show();
}
private void SmsTextBoxReadVisible() {
Intent dialogIntent = new Intent(ServiceStart.this, SmsBoxRead.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
getApplicationContext().startActivity(dialogIntent);
//Toast.makeText(getApplicationContext(), "ReadVisible", Toast.LENGTH_SHORT).show();
}
public static void messageReceiveListener(String newMessage , String phoneNumber){
if(popup_show_Swap==true)popup_show_icon.setImageResource(R.drawable.tick_new_msgn);
else popup_show_icon.setImageResource(R.drawable.not_new_msgn);
try {SmsBoxRead.messageReceiveListener(newMessage, phoneNumber);} catch (Exception e) {e.printStackTrace();}
subScriptStart();
}
public static void smsReadIconDisibleWithAnim() {
AnimationSet set = new AnimationSet(false);
RotateAnimation rt = new RotateAnimation(0, 90,ConfigMain.sms_read_width/2,ConfigMain.sms_read_height/2);
rt.setDuration((int)(700*ConfigMain.animationSpeed));
TranslateAnimation tr = new TranslateAnimation(0, ConfigMain.sms_read_width, 0, -ConfigMain.sms_read_height);
tr.setDuration((int)(700*ConfigMain.animationSpeed));
AlphaAnimation al = new AlphaAnimation(1, 0);
al.setDuration((int)(700*ConfigMain.animationSpeed));
set.addAnimation(rt);
set.addAnimation(tr);
set.addAnimation(al);
sms_RW_icon.startAnimation(set);
set.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0) {}
public void onAnimationRepeat(Animation arg0) {}
@Override
public void onAnimationEnd(Animation arg0) {
sms_RW_icon.setVisibility(View.INVISIBLE);}});
}
private void smsReadIconVisibleWithAnim() {
int[] location = new int[2];
sms_read_overlay.setVisibility(View.VISIBLE);
popup_show_overlay.getLocationOnScreen(location);
WindowManager.LayoutParams params = (LayoutParams)sms_read_overlay.getLayoutParams();
params.x = location[0];
params.y = location[1]+ConfigMain.popup_show_height/2;
wm.updateViewLayout(sms_read_overlay, params);
AnimationSet set = new AnimationSet(false);
RotateAnimation rt = new RotateAnimation(90, 0,ConfigMain.sms_read_width/2,ConfigMain.sms_read_height/2);
rt.setDuration((int)(700*ConfigMain.animationSpeed));
TranslateAnimation tr = new TranslateAnimation(ConfigMain.sms_read_width, 0, -ConfigMain.sms_read_height, 0);
tr.setDuration((int)(700*ConfigMain.animationSpeed));
AlphaAnimation al = new AlphaAnimation(0, 1);
al.setDuration((int)(700*ConfigMain.animationSpeed));
set.addAnimation(rt);
set.addAnimation(tr);
set.addAnimation(al);
sms_RW_icon.startAnimation(set);
sms_RW_icon.setVisibility(View.VISIBLE);
}
private void smsReadWriteChangeIconWithAnim1() {
AnimationSet set = new AnimationSet(false);
RotateAnimation rt = new RotateAnimation(0,180,ConfigMain.sms_read_width/2,ConfigMain.sms_read_height/2);
rt.setDuration((int)(500*ConfigMain.animationSpeed));
AlphaAnimation al = new AlphaAnimation(1, 5/10);
al.setDuration((int)(500*ConfigMain.animationSpeed));
set.addAnimation(rt);
set.addAnimation(al);
sms_RW_icon.startAnimation(set);
sms_RW_icon.setVisibility(View.VISIBLE);
sms_read_Swap=!sms_read_Swap;
serviceButtonClicked=true;
set.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0) {
if(sms_read_Swap==true){
CountDownTimer cont = new CountDownTimer(500, 50) {
public void onTick(long arg0) {}
@Override
public void onFinish() {
SmsTextBoxReadVisible();
}
}.start();
if(SmsBoxWrite.smsBoxWriteHasRuning)SmsBoxWrite.popUpWindowWrite.dismiss();
if(SmsBoxWrite.popUpWindowWrite.isShowing())SmsBoxWrite.popUpWindowWrite.dismiss();
}else{
sms_read_overlay.setEnabled(false);
CountDownTimer cont = new CountDownTimer(500, 50) {
public void onTick(long arg0){}
@Override
public void onFinish() {
SmsTextBoxWriteVisible();
sms_read_overlay.setEnabled(true);
this.cancel();
}
}.start();
if(SmsBoxRead.smsBoxReadHasRuning)SmsBoxRead.popUpWindowRead.dismiss();
if(SmsBoxRead.popUpWindowRead.isShowing())SmsBoxRead.popUpWindowRead.dismiss();
}
}
public void onAnimationRepeat(Animation arg0) {}
@Override
public void onAnimationEnd(Animation arg0) {
smsReadWriteChangeIconWithAnim2();
if(sms_read_Swap==true)sms_RW_icon.setImageResource(R.drawable.sms_read);
else sms_RW_icon.setImageResource(R.drawable.sms_write);
}
});
}
private void smsReadWriteChangeIconWithAnim2() {
AnimationSet set = new AnimationSet(false);
RotateAnimation rt = new RotateAnimation(180,360,ConfigMain.sms_read_width/2,ConfigMain.sms_read_height/2);
rt.setDuration((int)(500*ConfigMain.animationSpeed));
AlphaAnimation al = new AlphaAnimation(0, 1);
al.setDuration((int)(500*ConfigMain.animationSpeed));
set.addAnimation(rt);
set.addAnimation(al);
sms_RW_icon.startAnimation(set);
sms_RW_icon.setVisibility(View.VISIBLE);
if(sms_read_Swap==true)sms_RW_icon.setImageResource(R.drawable.sms_read);
else sms_RW_icon.setImageResource(R.drawable.sms_write);
}
private void ApplyAppearances() {
//ConfigMain.sp = getApplicationContext().getSharedPreferences("Config",0);
ConfigMain.sms_read_width=ConfigMain.sp.getInt("smsReadWidth", 70);
ConfigMain.popup_show_height=ConfigMain.sp.getInt("popupShowHeight", 70);
ConfigMain.sms_read_height=ConfigMain.sp.getInt("smsReadHeight", 70);
ConfigMain.popup_show_width=ConfigMain.sp.getInt("popupShowWidth", 70);
ConfigMain.animationSpeed=ConfigMain.sp.getInt("animationSpeed", 1);
sms_RW_icon.getLayoutParams().height=ConfigMain.sms_read_height; // Get from User!!!!!!!!!!!
sms_RW_icon.getLayoutParams().width=ConfigMain.sms_read_width;
popup_show_icon.getLayoutParams().height=ConfigMain.popup_show_height;
popup_show_icon.getLayoutParams().width=ConfigMain.popup_show_width;
}
private static void subScriptStart() {
String newMessage, contact;
newMessage = IncommingSms.messagE;
contact = SmsBoxRead.getPhoneName(IncommingSms.phoneNumbeR);
subScriptTextView.setText(Html.fromHtml("<font color=red>"+contact+ ": </font><font color=blue>" +newMessage+"</font><br><br/>"));
subScript_overlay.setVisibility(View.VISIBLE);
subScriptTextView.setVisibility(View.VISIBLE);
int subScriptWidth = (int) ((newMessage.length()+contact.length())*(ConfigMain.width/35));
WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
params.width = (int) (subScriptWidth*1.5);
if(trueTopFalseBottom==true) params.gravity = Gravity.TOP;
if(trueTopFalseBottom==false) params.gravity = Gravity.BOTTOM;
params.x = 0;
params.y = 0;
wm.updateViewLayout(subScript_overlay, params);
float toXdelta = -(subScriptWidth-ConfigMain.width);
//Toast.makeText(ConfigMain.con1, "subScriptWidth: "+subScriptWidth +"\n ConfigMain.width:"+ConfigMain.width, Toast.LENGTH_LONG).show();
TranslateAnimation tr = new TranslateAnimation(ConfigMain.width, -subScriptWidth, 0, 0);
tr.setDuration(10000);
subScriptTextView.startAnimation(tr);
CountDownTimer cont = new CountDownTimer(10000, 1000) {
public void onTick(long arg0) {}
@Override
public void onFinish() {
Toast.makeText(ConfigMain.con1, "end", Toast.LENGTH_SHORT).show();
subScriptTextView.setVisibility(View.INVISIBLE);
subScript_overlay.setVisibility(View.INVISIBLE);
this.cancel(); }
}.start();
}
}
谢谢
更新:
舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.MDGH.Always_Accessible"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ConfigMain"
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=".ServiceStart" android:stopWithTask="false"> </service>
<activity android:name=".SmsBoxRead"
android:theme="@android:style/Theme.Translucent.NoTitleBar"> </activity>
<activity android:name=".SmsBoxWrite"
android:theme="@android:style/Theme.Translucent.NoTitleBar"> </activity>
<receiver android:name=".IncommingSms">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
</manifest>
尝试创建
服务时出错
您尚未实现onStartCommand()
,因此默认实现返回START\u STICKY
。这意味着一旦您的服务启动,Android将重新启动它(如果需要),直到服务停止
你的进程正在被Android扼杀。这几乎可以在Android决定需要资源的任何时候发生。然后,由于您的服务已从onStartCommand()
返回START\u STICKY
,因此Android将重新启动您的服务。Android为您的服务
类创建一个新实例,并在其上调用onCreate()
。在这种情况下,由于您的ConfigMain
尚未启动(自Android终止进程以来),ConfigMain.sp
为null
另外,不要这样做:
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
让Android在你的进程准备好的时候杀死它自己。这是很多代码,我想你很难找到人“只是为了好玩”地解析和调试它。你能不能试着把代码分解,直到你有一个最小的工作程序,在这个程序中仍然会出现错误?这样,获得帮助的机会就会大大增加。但是我不知道,我的代码的哪一部分没有问题,并且犯了这个错误,删除它只是想在这里提供帮助。也许有人不需要运行代码就能自己通读这些行并告诉你,第xxx行中的字母5是错误的(或类似的)-我会留下深刻印象。您应该开始一步一步地剥离代码,直到不再出现错误。然后将上一版本与错误进行比较,并将新版本与错误进行比较。这是背部疼痛吗?是的。顺便问一下:检查ApplyAppearances(ServiceStart.java:489)中发生了什么?谢谢您的帮助。我希望我能解决它。。。非常无聊、乏味和令人失望……:-(您已将此代码注释掉://ConfigMain.sp=getApplicationContext().getSharedReferences(“Config”,0);
。它看起来像ConfigMain.sp
是null
。ConfigMain.sp
在哪里初始化?您好,谢谢。我不知道如何创建onStart命令()那么它是如何工作的……请帮助我更多。非常感谢你的回答。添加这个我删除的int-pid=android.os.Process.myPid()
和android.os.Process.killProcess(pid)
,但不做任何更改。但我认为问题出在'onStartCommand()’正如你所说,谢谢,请解释更多我应该在‘onStartCommand()’中做什么非常感谢帮助你更多我需要知道你如何启动你的服务,它需要运行多长时间,以及你想如何停止它。请通过编辑你的帖子来解释这一点(不是在这里的评论中)。我更新了我的代码(添加从那里开始服务的第一个活动)再次感谢
package com.MDGH.Always_Accessible;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class ConfigMain extends Activity implements OnClickListener {
public static ToggleButton ActiveButton;
private ImageButton ExitButton;
private Intent intent;
public static Context con1;
public static int height,width;
public static float animationSpeed,fontSize;
public static int sms_read_width,popup_show_width,sms_read_height,popup_show_height, popUpHeight ,popUpWidth;
public static boolean firstExecute=true;
public static SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.config_main);
sp = getApplicationContext().getSharedPreferences("Config",0);
ActiveButton = (ToggleButton)findViewById(R.id.configStart);
ExitButton = (ImageButton)findViewById(R.id.configExit);
intent = new Intent(ConfigMain.this, ServiceStart.class);
GetDisplayMeasures();
SetAppearances();
ActiveButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if(ActiveButton.isChecked()){
// Active Commands
Editor editor = sp.edit();
editor.putBoolean("isActive",true);
editor.commit();
startService(intent);
finish();
}else{
// DeActive Commands
Editor editor = sp.edit();
editor.putBoolean("isActive",false);
editor.commit();
stopService(intent);
}
}
});
}
private void SetAppearances(){
ActiveButton.setChecked(ConfigMain.sp.getBoolean("isActive", false));
fontSize=ConfigMain.sp.getFloat("fontSize", 1);
}
private void GetDisplayMeasures() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
height= dm.heightPixels;
width= dm.widthPixels;
}
@Override
public void onClick(View clickId) {
switch (clickId.getId()) {
case R.id.configExit:
ConfigMain.this.finish();
break;}
}
@Override
protected void onStart() {
con1 = getApplicationContext();
super.onStart();
}
}
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);