Reputation: 211
this is my service class in my app. this is a app that shows two icon (imageView) as system_alert_window that these icons can open two activity. when start service, after several minutes or after execute a activity from service and then comeback, after several seconds service has been force close two times.
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();
}
}
when force closed service shown these errors:
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
thanks
Update:
Manifest:
<?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>
Update2:
My first Activity:
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();
}
}
Upvotes: 0
Views: 70
Reputation: 95618
The error occurs when trying to create the Service
.
You have not implemented onStartCommand()
, so the default implementation returns START_STICKY
. This means that once your service is started, Android will restart it (if necessary) until the service is stopped.
Your process is getting killed by Android. This can happen pretty much anytime that Android decides it needs the resources. Then, because your service has returned START_STICKY
from onStartCommand()
, Android will restart your service. Android creates a new instance of your Service
class and calls onCreate()
on it. In this case, since your ConfigMain
hasn't been started yet (since Android killed the process), ConfigMain.sp
is null
.
Also, don't do this:
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
Just let Android kill your process itself when it is ready to.
Upvotes: 2