rk3288-8.1长按应用设置该应用开机自启

it2023-01-06  64

前言

需求:在设置–>应用界面长按某应用弹出是否开机自启对话框,确认之后该应用开机自启


代码如下(示例):

diff --git a/res/values-zh-rCN/strings.xml b/res/values-zh-rCN/strings.xml index 0062f7c..6f2c829 100755 --- a/res/values-zh-rCN/strings.xml +++ b/res/values-zh-rCN/strings.xml @@ -3735,4 +3735,5 @@ <string name="confirm_dialog_title">保存确认</string> <string name="confirm_dialog_message">将会取消</string> <string name="hide_systembar_settings_title">隐藏状态栏</string> + <string name="boot_apk_title">确定要开机自启该应用吗?</string> </resources> diff --git a/res/values/strings.xml b/res/values/strings.xml index 17cab20..f30e46d 100755 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9123,4 +9123,5 @@ <string name="confirm_dialog_title">Save Confirm</string> <string name="confirm_dialog_message">Will cancel it</string> <string name="hide_systembar_settings_title">hide the statusbar</string> + <string name="boot_apk_title">Are you sure to boot this apk?</string> </resources> diff --git a/src/com/android/settings/applications/ManageApplications.java b/src/com/android/settings/applications/ManageApplications.java index deda92a..9b1c553 100644 --- a/src/com/android/settings/applications/ManageApplications.java +++ b/src/com/android/settings/applications/ManageApplications.java @@ -46,6 +46,7 @@ import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; +import android.widget.AdapterView.OnItemLongClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; @@ -97,6 +98,9 @@ import java.util.Collections; import java.util.Comparator; import java.util.Locale; import java.util.Set; +import android.content.SharedPreferences; +import android.app.AlertDialog; +import android.content.DialogInterface; /** * Activity to pick an application that will be used to display installation information and @@ -105,7 +109,7 @@ import java.util.Set; * intent. */ public class ManageApplications extends InstrumentedPreferenceFragment - implements OnItemClickListener, OnItemSelectedListener { + implements OnItemClickListener, OnItemSelectedListener, OnItemLongClickListener { static final String TAG = "ManageApplications"; static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); @@ -364,6 +368,7 @@ public class ManageApplications extends InstrumentedPreferenceFragment mRootView = inflater.inflate(R.layout.manage_applications_apps, null); mLoadingContainer = mRootView.findViewById(R.id.loading_container); mListContainer = mRootView.findViewById(R.id.list_container); + mSharedPreference = getContext().getSharedPreferences("time",getContext().MODE_PRIVATE); if (mListContainer != null) { // Create adapter and list view here View emptyView = mListContainer.findViewById(com.android.internal.R.id.empty); @@ -372,6 +377,7 @@ public class ManageApplications extends InstrumentedPreferenceFragment lv.setEmptyView(emptyView); } lv.setOnItemClickListener(this); + lv.setOnItemLongClickListener(this); lv.setSaveEnabled(true); lv.setItemsCanFocus(true); lv.setTextFilterEnabled(true); @@ -735,7 +741,7 @@ public class ManageApplications extends InstrumentedPreferenceFragment updateOptionsMenu(); return true; } - + private SharedPreferences mSharedPreference; @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mApplications == null) { @@ -751,9 +757,43 @@ public class ManageApplications extends InstrumentedPreferenceFragment mApplications.mExtraViewController.onClick(this); } } + @Override + public boolean onItemLongClick(AdapterView<?> parent, View view, int position, + long id) { + ApplicationsState.AppEntry entry = mApplications.getAppEntry(position); + mCurrentPkgName = entry.info.packageName; + Log.d("SVVV","--------------onItemLongClick=="+mCurrentPkgName); + BootDialog(); + return true; + } + + private void BootDialog(){ + final AlertDialog.Builder normalDialog = + new AlertDialog.Builder(getActivity()); + normalDialog.setMessage(R.string.boot_apk_title); + normalDialog.setPositiveButton(R.string.okay, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + //...To-do + SharedPreferences.Editor editor = mSharedPreference.edit(); + editor.putString("bootapk",mCurrentPkgName); + editor.commit(); + } + }); + normalDialog.setNegativeButton(R.string.cancel, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + //...To-do + } + }); + normalDialog.show(); + } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { + Log.d("SVVV","------------onItemSelected------2222222"); mFilter = mFilterAdapter.getFilter(position); mApplications.setFilter(mFilter); if (DEBUG) Log.d(TAG, "Selecting filter " + mFilter); diff --git a/src/com/android/settings/shutdown/ShutdownBroadcastReceiver.java b/src/com/android/settings/shutdown/ShutdownBroadcastReceiver.java index 2dfe8b8..05e2c4f 100644 --- a/src/com/android/settings/shutdown/ShutdownBroadcastReceiver.java +++ b/src/com/android/settings/shutdown/ShutdownBroadcastReceiver.java @@ -40,6 +40,18 @@ public class ShutdownBroadcastReceiver extends BroadcastReceiver{ if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ + String bootapk = myshaPreferences.getString("bootapk", null); + Log.d("SVVV","-----------bootapk=="+bootapk); + if(bootapk.equals("")&& bootapk==null){ + Log.d("SVVV","----------------bootapk is null "); + }else{ + try { + Intent intent_apk = context.getPackageManager().getLaunchIntentForPackage(bootapk); + context.startActivity(intent_apk); + } catch (Exception e) { + // TODO: handle exception + } + }
最新回复(0)