添加主服务
This commit is contained in:
		| @@ -64,7 +64,7 @@ dependencies { | ||||
|     api 'com.google.android.material:material:1.1.0' | ||||
|      | ||||
|     api 'cc.winboll.studio:libapputils:9.3.2' | ||||
|     api 'cc.winboll.studio:libappbase:1.4.1' | ||||
|     api 'cc.winboll.studio:libappbase:1.5.0' | ||||
|      | ||||
|     api fileTree(dir: 'libs', include: ['*.jar']) | ||||
| } | ||||
|   | ||||
| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Wed Feb 12 15:27:12 GMT 2025 | ||||
| #Wed Feb 12 23:28:05 GMT 2025 | ||||
| stageCount=0 | ||||
| libraryProject=winboll-shared | ||||
| baseVersion=1.0 | ||||
| publishVersion=1.0.0 | ||||
| buildCount=2 | ||||
| buildCount=6 | ||||
| baseBetaVersion=1.0.1 | ||||
|   | ||||
| @@ -42,7 +42,18 @@ | ||||
|  | ||||
|         </provider> | ||||
|  | ||||
|         <service android:name="cc.winboll.studio.contacts.services.MainService"/> | ||||
|  | ||||
|         <receiver android:name="cc.winboll.studio.contacts.receivers.MainReceiver"> | ||||
|  | ||||
|             <intent-filter> | ||||
|  | ||||
|                 <action android:name="cc.winboll.studio.contacts.receivers.MainReceiver"/> | ||||
|  | ||||
|             </intent-filter> | ||||
|  | ||||
|         </receiver> | ||||
|  | ||||
|     </application> | ||||
|  | ||||
| </manifest> | ||||
| </manifest> | ||||
| @@ -1,28 +1,23 @@ | ||||
| package cc.winboll.studio.contacts; | ||||
|  | ||||
| import android.content.Intent; | ||||
| import android.net.Uri; | ||||
| import android.os.Bundle; | ||||
| import android.text.TextUtils; | ||||
| import android.view.Menu; | ||||
| import android.view.MenuItem; | ||||
| import android.view.View; | ||||
| import android.widget.CheckBox; | ||||
| import androidx.appcompat.app.AppCompatActivity; | ||||
| import androidx.appcompat.widget.Toolbar; | ||||
| import cc.winboll.studio.contacts.BuildConfig; | ||||
| import cc.winboll.studio.contacts.R; | ||||
| import cc.winboll.studio.contacts.beans.MainServiceBean; | ||||
| import cc.winboll.studio.contacts.services.MainService; | ||||
| import cc.winboll.studio.libappbase.LogUtils; | ||||
| import cc.winboll.studio.libapputils.app.AboutActivityFactory; | ||||
| import cc.winboll.studio.libapputils.app.IWinBollActivity; | ||||
| import cc.winboll.studio.libapputils.app.WinBollActivityManager; | ||||
| import cc.winboll.studio.libapputils.bean.APPInfo; | ||||
| import cc.winboll.studio.libapputils.util.UriUtils; | ||||
| import cc.winboll.studio.libapputils.view.StringToQrCodeView; | ||||
| import cc.winboll.studio.libapputils.view.YesNoAlertDialog; | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.nio.file.Files; | ||||
| import java.nio.file.Paths; | ||||
| import java.util.UUID; | ||||
| import cc.winboll.studio.libapputils.app.AboutActivityFactory; | ||||
|  | ||||
| final public class MainActivity extends AppCompatActivity implements IWinBollActivity { | ||||
|  | ||||
| @@ -32,12 +27,14 @@ final public class MainActivity extends AppCompatActivity implements IWinBollAct | ||||
|     public static final int REQUEST_ABOUT_ACTIVITY = 1; | ||||
|  | ||||
|     Toolbar mToolbar; | ||||
|      | ||||
|     CheckBox cbMainService; | ||||
|     MainServiceBean mMainServiceBean; | ||||
|  | ||||
|     @Override | ||||
|     public AppCompatActivity getActivity() { | ||||
|         return this; | ||||
|     } | ||||
|      | ||||
|  | ||||
|     @Override | ||||
|     public APPInfo getAppInfo() { | ||||
|         String szBranchName = "contacts"; | ||||
| @@ -73,9 +70,27 @@ final public class MainActivity extends AppCompatActivity implements IWinBollAct | ||||
|             getSupportActionBar().setDisplayHomeAsUpEnabled(true); | ||||
|         } | ||||
|         getSupportActionBar().setSubtitle(getTag()); | ||||
|          | ||||
|  | ||||
|         //ToastUtils.show("WinBollUI_TYPE " + WinBollApplication.getWinBollUI_TYPE()); | ||||
|         LogUtils.d(TAG, "BuildConfig.DEBUG : " + Boolean.toString(BuildConfig.DEBUG)); | ||||
|  | ||||
|         mMainServiceBean = MainServiceBean.loadBean(this, MainServiceBean.class); | ||||
|         if (mMainServiceBean == null) { | ||||
|             mMainServiceBean = new MainServiceBean(); | ||||
|         } | ||||
|         cbMainService = findViewById(R.id.activitymainCheckBox1); | ||||
|         cbMainService.setChecked(mMainServiceBean.isEnable()); | ||||
|         cbMainService.setOnClickListener(new View.OnClickListener(){ | ||||
|                 @Override | ||||
|                 public void onClick(View view) { | ||||
|                      | ||||
|                     if (cbMainService.isChecked()) { | ||||
|                         MainService.startISOSService(MainActivity.this); | ||||
|                     } else { | ||||
|                         MainService.stopISOSService(MainActivity.this); | ||||
|                     } | ||||
|                 } | ||||
|             }); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -146,7 +161,7 @@ final public class MainActivity extends AppCompatActivity implements IWinBollAct | ||||
|     public boolean isEnableDisplayHomeAsUp() { | ||||
|         return false; | ||||
|     } | ||||
|      | ||||
|  | ||||
|     @Override | ||||
|     public void onBackPressed() { | ||||
|         exit(); | ||||
|   | ||||
| @@ -0,0 +1,68 @@ | ||||
| package cc.winboll.studio.contacts.beans; | ||||
|  | ||||
| /** | ||||
|  * @Author ZhanGSKen@AliYun.Com | ||||
|  * @Date 2025/02/13 07:06:13 | ||||
|  */ | ||||
| import android.util.JsonReader; | ||||
| import android.util.JsonWriter; | ||||
| import cc.winboll.studio.libappbase.BaseBean; | ||||
| import java.io.IOException; | ||||
|  | ||||
| public class MainServiceBean extends BaseBean { | ||||
|  | ||||
|     public static final String TAG = "MainServiceBean"; | ||||
|  | ||||
|     boolean isEnable; | ||||
|  | ||||
|     public MainServiceBean() { | ||||
|         this.isEnable = false; | ||||
|     } | ||||
|  | ||||
|     public void setIsEnable(boolean isEnable) { | ||||
|         this.isEnable = isEnable; | ||||
|     } | ||||
|  | ||||
|     public boolean isEnable() { | ||||
|         return isEnable; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String getName() { | ||||
|         return MainServiceBean.class.getName(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void writeThisToJsonWriter(JsonWriter jsonWriter) throws IOException { | ||||
|         super.writeThisToJsonWriter(jsonWriter); | ||||
|         MainServiceBean bean = this; | ||||
|         jsonWriter.name("isEnable").value(bean.isEnable()); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean initObjectsFromJsonReader(JsonReader jsonReader, String name) throws IOException { | ||||
|         if (super.initObjectsFromJsonReader(jsonReader, name)) { return true; } else { | ||||
|             if (name.equals("isEnable")) { | ||||
|                 setIsEnable(jsonReader.nextBoolean()); | ||||
|             } else { | ||||
|                 return false; | ||||
|             } | ||||
|         } | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public BaseBean readBeanFromJsonReader(JsonReader jsonReader) throws IOException { | ||||
|         jsonReader.beginObject(); | ||||
|         while (jsonReader.hasNext()) { | ||||
|             String name = jsonReader.nextName(); | ||||
|             if (!initObjectsFromJsonReader(jsonReader, name)) { | ||||
|                 jsonReader.skipValue(); | ||||
|             } | ||||
|         } | ||||
|         // 结束 JSON 对象 | ||||
|         jsonReader.endObject(); | ||||
|         return this; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,28 @@ | ||||
| package cc.winboll.studio.contacts.receivers; | ||||
|  | ||||
| import android.content.BroadcastReceiver; | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| import com.hjq.toast.ToastUtils; | ||||
|  | ||||
| /** | ||||
|  * @Author ZhanGSKen@AliYun.Com | ||||
|  * @Date 2025/02/13 06:58:04 | ||||
|  * @Describe 主要广播接收器 | ||||
|  */ | ||||
| public class MainReceiver extends BroadcastReceiver { | ||||
|      | ||||
|     public static final String TAG = "MainReceiver"; | ||||
|     public static final String ACTION_BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED"; | ||||
|      | ||||
|     @Override | ||||
|     public void onReceive(Context context, Intent intent) { | ||||
|         String szAction = intent.getAction(); | ||||
|         if (szAction.equals(ACTION_BOOT_COMPLETED)) { | ||||
|             ToastUtils.show("ACTION_BOOT_COMPLETED"); | ||||
|         } else { | ||||
|             ToastUtils.show("szAction"); | ||||
|         } | ||||
|     } | ||||
|      | ||||
| } | ||||
| @@ -0,0 +1,142 @@ | ||||
| package cc.winboll.studio.contacts.services; | ||||
|  | ||||
| import android.app.Service; | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| import android.os.IBinder; | ||||
| import cc.winboll.studio.contacts.beans.MainServiceBean; | ||||
| import cc.winboll.studio.libappbase.ISOSAPP; | ||||
| import cc.winboll.studio.libappbase.ISOSService; | ||||
| import cc.winboll.studio.libappbase.LogUtils; | ||||
| import com.hjq.toast.ToastUtils; | ||||
|  | ||||
| /** | ||||
|  * @Author ZhanGSKen@AliYun.Com | ||||
|  * @Date 2025/02/13 06:56:41 | ||||
|  * @Describe 拨号主服务 | ||||
|  */ | ||||
| public class MainService extends Service implements ISOSService { | ||||
|      | ||||
|     public static final String TAG = "MainService"; | ||||
|      | ||||
|     public static final String ACTION_ENABLE = MainService.class.getName() + ".ACTION_ENABLE"; | ||||
|     public static final String ACTION_DISABLE = MainService.class.getName() + ".ACTION_DISABLE"; | ||||
|  | ||||
|     MainServiceBean mMainServiceBean; | ||||
|     static MainThread _MainThread; | ||||
|     public static synchronized MainThread getMainThreadInstance() { | ||||
|         if (_MainThread == null) { | ||||
|             _MainThread = new MainThread(); | ||||
|         } | ||||
|         return _MainThread; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public IBinder onBind(Intent intent) { | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onCreate() { | ||||
|         super.onCreate(); | ||||
|         LogUtils.d(TAG, "onCreate"); | ||||
|         mMainServiceBean = MainServiceBean.loadBean(this, MainServiceBean.class); | ||||
|         runMainThread(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int onStartCommand(Intent intent, int flags, int startId) { | ||||
|         LogUtils.d(TAG, "onStartCommand"); | ||||
|         if (intent.getBooleanExtra(ISOSService.EXTRA_ENABLE, false)) { | ||||
|             LogUtils.d(TAG, "onStartCommand enable service"); | ||||
|             mMainServiceBean.setIsEnable(true); | ||||
|             MainServiceBean.saveBean(this, mMainServiceBean); | ||||
|         } | ||||
|  | ||||
|         runMainThread(); | ||||
|  | ||||
|         //return super.onStartCommand(intent, flags, startId); | ||||
|         return mMainServiceBean.isEnable() ? Service.START_STICKY: super.onStartCommand(intent, flags, startId); | ||||
|     } | ||||
|  | ||||
|     void runMainThread() { | ||||
|         mMainServiceBean = MainServiceBean.loadBean(this, MainServiceBean.class); | ||||
|         if (mMainServiceBean.isEnable() | ||||
|             && _MainThread == null) { | ||||
|             getMainThreadInstance().start(); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public Intent getISOSServiceIntentWhichAskForHelp() { | ||||
|         Intent intentService = new Intent(); | ||||
|         intentService.putExtra(ISOSAPP.EXTRA_PACKAGE, this.getPackageName()); | ||||
|         intentService.putExtra(ISOSAPP.EXTRA_SERVICE, this.getClass().getName()); | ||||
|         return intentService; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean isEnable() { | ||||
|         mMainServiceBean = MainServiceBean.loadBean(this, MainServiceBean.class); | ||||
|         return mMainServiceBean.isEnable(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onDestroy() { | ||||
|         super.onDestroy(); | ||||
|         LogUtils.d(TAG, "onDestroy"); | ||||
|         mMainServiceBean = MainServiceBean.loadBean(this, MainServiceBean.class); | ||||
|         if (mMainServiceBean.isEnable()) { | ||||
|             LogUtils.d(TAG, "mSimpleOperateSignalCenterServiceBean.isEnable()"); | ||||
|             ISOSAPP iSOSAPP = (ISOSAPP)getApplication(); | ||||
|             iSOSAPP.helpISOSService(getISOSServiceIntentWhichAskForHelp()); | ||||
|         }  | ||||
|         if (_MainThread != null) { | ||||
|             _MainThread.isExist = true; | ||||
|             _MainThread = null; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static void stopISOSService(Context context) { | ||||
|         LogUtils.d(TAG, "stopISOSService"); | ||||
|         MainServiceBean bean = new MainServiceBean(); | ||||
|         bean.setIsEnable(false); | ||||
|         MainServiceBean.saveBean(context, bean); | ||||
|         context.stopService(new Intent(context, MainService.class)); | ||||
|     } | ||||
|  | ||||
|     public static void startISOSService(Context context) { | ||||
|         LogUtils.d(TAG, "startISOSService"); | ||||
|         MainServiceBean bean = new MainServiceBean(); | ||||
|         bean.setIsEnable(true); | ||||
|         MainServiceBean.saveBean(context, bean); | ||||
|         context.startService(new Intent(context, MainService.class)); | ||||
|     } | ||||
|  | ||||
|     static class MainThread extends Thread { | ||||
|         volatile boolean isExist = false; | ||||
|  | ||||
|         public void setIsExist(boolean isExist) { | ||||
|             this.isExist = isExist; | ||||
|         } | ||||
|  | ||||
|         public boolean isExist() { | ||||
|             return isExist; | ||||
|         } | ||||
|  | ||||
|         @Override | ||||
|         public void run() { | ||||
|             super.run(); | ||||
|             while (!isExist) { | ||||
|                 LogUtils.d(TAG, "run"); | ||||
|                 ToastUtils.show("run"); | ||||
|                 try { | ||||
|                     sleep(1000); | ||||
|                 } catch (InterruptedException e) { | ||||
|                     LogUtils.d(TAG, e, Thread.currentThread().getStackTrace()); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|     } | ||||
| } | ||||
| @@ -2,7 +2,7 @@ | ||||
| <LinearLayout | ||||
| 	xmlns:android="http://schemas.android.com/apk/res/android" | ||||
| 	xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
|     android:orientation="vertical" | ||||
| 	android:orientation="vertical" | ||||
| 	android:layout_width="match_parent" | ||||
| 	android:layout_height="match_parent"> | ||||
|  | ||||
| @@ -19,10 +19,18 @@ | ||||
| 		android:layout_weight="1.0" | ||||
| 		android:background="#FFEEEEEE"> | ||||
|  | ||||
| 		<TextView | ||||
| 			android:layout_width="wrap_content" | ||||
| 			android:layout_height="wrap_content" | ||||
| 			android:text="Hello, WinBoll!"/> | ||||
| 		<LinearLayout | ||||
| 			android:orientation="horizontal" | ||||
| 			android:layout_width="match_parent" | ||||
| 			android:layout_height="wrap_content"> | ||||
|  | ||||
| 			<CheckBox | ||||
| 				android:layout_width="wrap_content" | ||||
| 				android:layout_height="wrap_content" | ||||
| 				android:text="MainService" | ||||
| 				android:id="@+id/activitymainCheckBox1"/> | ||||
|  | ||||
| 		</LinearLayout> | ||||
|  | ||||
| 	</LinearLayout> | ||||
|  | ||||
|   | ||||
| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Wed Feb 12 15:27:12 GMT 2025 | ||||
| #Wed Feb 12 23:28:05 GMT 2025 | ||||
| stageCount=0 | ||||
| libraryProject=winboll-shared | ||||
| baseVersion=1.0 | ||||
| publishVersion=1.0.0 | ||||
| buildCount=2 | ||||
| buildCount=6 | ||||
| baseBetaVersion=1.0.1 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen