我有一个简单的应用程序,每天通过一次通知向用户提供动机报价。单击通知时,用户将进入显示报价的活动。
主要活动没有gui,它启动Serv1然后调用finish()。 word1类非常简单,它有一个String数组的引号,它检查一年中的某一天,然后转到该索引并显示引号。
我认为Android系统可以在需要更多内存时随时随地杀死它。我认为我的选择可能是
将它设为用户所知的前台服务,因此不能让系统杀死?
使用start sticky,我在Settings-> dev options->中检查了我的手机。过程统计数据,我看到Serv1运行了大约4天,但今天它不在那里。是否有一个系统保持开始粘贴的列表?它被杀死到重新启动之间的时间长度。那就是我在m代码中正确使用它。
或定期开启?通过broadcastReciever检查它是否正在运行..这是一个有点模糊的地方,如果这被杀死会导致什么?
我对Android很新,所以任何帮助都会很棒...
public class Serv1 extends Service {
private PendingIntent pendingIntent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 18);
Intent myIntent = new Intent(Serv1.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(Serv1.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) this
.getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
return START_REDELIVER_INTENT;
}
}
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
public class MyAlarmService extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, startId, startId);
Intent intent1 = new Intent(this.getApplicationContext(), Word.class);
PendingIntent showWord = PendingIntent.getActivity(this, 0, intent1, 0);
Notification notification = new NotificationCompat.Builder(this)
.setTicker("Daily Word").setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Daily Word")
.setContentText("Your Word is ready")
.setContentIntent(showWord).setAutoCancel(true).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
stopSelf();
return START_REDELIVER_INTENT;
}
}
答案 0 :(得分:0)
试试这个:
public class RefreshService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private AudioManager mAudioManager;
private DbAdapter mDbHelper;
private String user = null;
private String pass = null;
public class RefreshBinder extends Binder {
RefreshService getService() {
return RefreshService.this;
}
}
private static final int MAXIMUM_NOTIFY_EVENTS = 7;
private final IBinder binder = new RefreshBinder();
private final Handler handler = new Handler();
private final Runnable refresher = new Runnable() {
public void run() {
refresh();
}
};
private final Runnable worker= new Runnable() {
public void run() {
doWork();
}
};
private BroadcastReceiver screenOffReceiver;
private boolean screenOn = true;
private BroadcastReceiver screenOnReceiver;
@Override
public IBinder onBind(Intent arg0) {
Log.i(RefreshService.class.getName(), "onBind");
return binder;
}
@Override
public void onCreate() {
super.onCreate();
screenOnReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("RefreshService", "Screen on");
refresh();
}
};
registerReceiver(screenOnReceiver, new IntentFilter(
Intent.ACTION_SCREEN_ON));
screenOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("RefreshService", "Screen off");
}
};
registerReceiver(screenOffReceiver, new IntentFilter(
Intent.ACTION_SCREEN_OFF));
scheduleNext();
scheduleNextPost();
}
@Override
public void onDestroy() {
Log.i(RefreshService.class.getName(), "onDestroy");
if (screenOnReceiver != null) {
unregisterReceiver(screenOnReceiver);
}
if (screenOffReceiver != null) {
unregisterReceiver(screenOffReceiver);
}
if (handler != null)
handler.removeCallbacks(refresher);
}
@Override
public boolean onUnbind(Intent arg) {
Log.i(RefreshService.class.getName(), "onUnbind");
return true;
}
public void refreshNext() {
scheduleNext();
}
public void refresh() {
Log.i("RefreshService", "Refreshing");
refreshWidgets();
scheduleNext();
}
public void doWork() {
doTheWork();
scheduleNextPost();
}
private void doTheWork() {
Log.d("RefreshService", "Refreshing ");
}
/**
* Note: The point of doing this here is to allow user control over the
* update frequency
*/
private void refreshWidgets() {
}
private void scheduleNext() {
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
long defaufaultFrequency = getLong(
getResources().getText(R.string.refresh_frequency_default)
.toString(), 30000);
handler.postDelayed(refresher, preferences.getLong(getResources()
.getText(R.string.refresh_frequency_key).toString(),
defaufaultFrequency));
}
private void scheduleNextPost() {
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
handler.postDelayed(worker,
preferences
.getLong(
getResources().getText(
R.string.post_frequency_key)
.toString(),
getLong(getResources().getText(
R.string.post_frequency_default)
.toString(), 30000)));
}
private long getLong(String string, long defaultValue) {
long number;
try {
number = Long.parseLong(string);
} catch (NumberFormatException e) {
number = defaultValue;
}
return number;
}
private int getInt(String string, int defaultValue) {
int number;
try {
number = Integer.parseInt(string);
} catch (NumberFormatException e) {
number = defaultValue;
}
return number;
}
protected void processStatus(Integer status) {
}
}
答案 1 :(得分:0)
将以下内容添加到Serv1类似乎可行吗?
public void onDestroy() {
Intent reStart = new Intent(Serv1.this, Serv1.class);
startService(reStart);
}