状态:
非常感谢所有在第一部分和这里提供帮助和指导的人!我根据研究和得到的帮助编写了代码,并将工作代码放在EDIT-1中。欢迎批评以使代码更好。
情景:
我在第一部分中提出了问题,但由于某种原因,我无法坚持使用具有真正集成和健全性的代码来制定设计和正确策略。
这是一个冗长的问题,问题或答案不能在一个回合中得出结论或最终确定,因此我制作了这个第二部分作为概要。
也许我无能或只是阅读了太多分散的文档和不同策略的答案,或者答案具有不同的观点/编码风格。
问题:
这就是我想要并最终得出结论的内容,经过观察不同的答案:
需要每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.
请问有没有人能够全面地告诉我应该使用哪种代码?我正在做研究,非常感谢但也头痛不已。
请求:
只有您确信并且有经验时,请回答此问题。
编辑-1-开始:
这是我迄今为止制作的内容,请随意评论或批评。
启动时运行的引导程序类。
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
}
});
Menifiest
<?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>
< p > 编辑-1-结束。