Status:
非常感谢所有在这里和第 1 部分提供帮助和指导的人!我已经根据研究和提供的帮助制作了代码,并将该工作代码放在 EDIT-1中。欢迎批评以使代码更好。
Scenario:
我问了第 1 部分中提到的问题,但由于某种原因,我一直无法使用具有真正集成和健全性的代码制定设计和正确的策略。
这是一个冗长的问题,问题或答案可能仅在一集中就可以结束或最终确定,因此我将第二部分作为概要。
可能是我不称职或只是打扰阅读这么多分散的文档和不同策略的答案,或者答案具有不同的视图/编码风格。
第 1部分第 1 部分持久性前台 android 服务,由 UI 启动,也可以在睡眠模式下工作,也可以在手机重启时启动
Question:
这是我想要并在观察不同答案后最终得出的结论:
需要每 15 分钟运行一次代码(即使手机处于睡眠状态)。我认为需要唤醒锁吗?
//AT boot, check shared preferences to see boolean "serviceEnabled"?
//if true, set alarm manager to run a service every 15 minuts.
//if false, do nothing.
//On "enable" button clicked.
//make "serviceEnabled" boolean true in shared preferences.
//start alarm manager to run a service every 15 minuts.
//on "Disable" button clicked.
//make "serviceEnabled" boolean false in shared preferences.
//stop alarm manager and deregister it to run ever.
任何人都可以全面地告诉...我应该使用什么代码...?我非常感谢研究头痛。
Requests:
请仅在您自信且有经验知道自己在做什么的情况下回答。
EDIT-1-Start:
这是我到目前为止所做的。请随时发表评论或批评。
在引导时运行的引导程序类。
public class Booter extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.e("boot-status","BOOT_COMPLETED=================================================");
// SharedPreferences prefs = context.getSharedPreferences("$MYPACKAGE_preferences",0);
// if (prefs.getBoolean("startatboot",false)) {
if(true){
Intent updateIntent = new Intent();
updateIntent.setClass(context, TheService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, updateIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, java.lang.System.currentTimeMillis()+5000,5000, pendingIntent);
}
}
}
}
服务等级
public class TheService extends Service{
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
PowerManager pm;
PowerManager.WakeLock wl;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
startForeground(1, new Notification());
////// will do all my stuff here on in the method onStart() or onCreat()?
Log.e("app-status","ping ====================================================");
new Thread(new Runnable() {
@Override
public void run() {
wl.release();
stopSelf();
}
}).start();
return START_STICKY;
}
@Override
public void onDestroy() {
stop();
}
public void stop(){
//if running
// stop
// make vars as false
// do some stopping stuff
stopForeground(true);
}
}
GUI启动/停止
public class SettingsActivity extends Activity {
// some code to initialize things
buttonStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent updateIntent = new Intent();
updateIntent.setClass(SettingsActivity.this, TheService.class);
PendingIntent pendingIntent = PendingIntent.getService(SettingsActivity.this, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)SettingsActivity.this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
//make sharepred boolean as false
}
});
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent updateIntent = new Intent();
updateIntent.setClass(SettingsActivity.this, TheService.class);
PendingIntent pendingIntent = PendingIntent.getService(SettingsActivity.this, 0, updateIntent, 0);
AlarmManager alarmManager = (AlarmManager)SettingsActivity.this.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, java.lang.System.currentTimeMillis()+5000,5000, pendingIntent);
//make shared prefs boolean as true
}
});
梅尼菲斯特
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> // irrelevent here
<uses-permission android:name="android.permission.INTERNET" /> // my app uses these though in service class.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myapp.SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
<service android:name=".TheService" />
<receiver android:name=".Booter" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application>
</manifest>
EDIT-1-END.