为什么几秒后强制关闭服务?

时间:2015-10-09 08:55:38

标签: android android-service

这是我的应用中的服务类。 这是一个显示两个图标(imageView)作为system_alert_window的应用程序,这些图标可以打开两个活动。 当启动服务时,几分钟后或者从服务执行一个活动然后回来后,几秒钟后服务已经强制关闭两次。

.ng-invalid{
  border: 1px solid red;
}

强制关闭服务时显示这些错误:

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();




        }

}

谢谢

更新

清单:

10-09 11:37:24.730: E/AndroidRuntime(13444): FATAL EXCEPTION: main
10-09 11:37:24.730: E/AndroidRuntime(13444): java.lang.RuntimeException: Unable to create service com.MDGH.Always_Accessible.ServiceStart: java.lang.NullPointerException
10-09 11:37:24.730: E/AndroidRuntime(13444):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2407)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at android.app.ActivityThread.access$1600(ActivityThread.java:138)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at android.os.Looper.loop(Looper.java:137)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at android.app.ActivityThread.main(ActivityThread.java:4954)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at java.lang.reflect.Method.invokeNative(Native Method)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at java.lang.reflect.Method.invoke(Method.java:511)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at dalvik.system.NativeStart.main(Native Method)
10-09 11:37:24.730: E/AndroidRuntime(13444): Caused by: java.lang.NullPointerException
10-09 11:37:24.730: E/AndroidRuntime(13444):    at com.MDGH.Always_Accessible.ServiceStart.ApplyAppearances(ServiceStart.java:489)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at com.MDGH.Always_Accessible.ServiceStart.onCreate(ServiceStart.java:70)
10-09 11:37:24.730: E/AndroidRuntime(13444):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2397)
10-09 11:37:24.730: E/AndroidRuntime(13444):    ... 10 more

UPDATE2:

我的第一个活动:

<?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>

1 个答案:

答案 0 :(得分:2)

尝试创建Service时出错。

您尚未实现onStartCommand(),因此默认实现返回START_STICKY。这意味着一旦您的服务启动,Android将重新启动它(如果需要),直到服务停止。

你的进程被Android杀死了。这可以在Android决定需要资源的任何时候发生。然后,由于您的服务已从START_STICKY返回onStartCommand(),Android将重新启动您的服务。 Android会为您的Service课程创建一个新实例,并在其上调用onCreate()。在这种情况下,由于您的ConfigMain尚未启动(因为Android终止了该过程),ConfigMain.spnull

另外,不要这样做:

    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);

让Android在准备就绪时自行终止你的进程。