请问,我怎样才能把thread
从我的doInBackground AsyncTask
变成foreground
?我需要永远不会被杀死的service
,用Notify notification
来做thread
。我用谷歌搜索了一下我会用startForeground
,但我不知道怎么用。这是我的想法,但是当我杀死应用程序或手机进入睡眠状态时,thread
熄灭,Notify notification
不显示,然而foreground notification
总是打开。
public class NotifyService extends Service {
private DatabaseOp mDbHelper;
public Vibrator vibrator;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("NotifyService", "onStartCommand");
startForegroundNotification();
NotifyBackground nb = new NotifyBackground();
nb.execute();
return START_STICKY;
}
private void startForegroundNotification() {
Notification note = new Notification(R.drawable.ic_launcher,
"Can you hear the music?", System.currentTimeMillis());
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
note.setLatestEventInfo(this, "Fake Player",
"Now Playing: "Ummmm, Nothing"", pi);
note.flags |= Notification.FLAG_NO_CLEAR;
startForeground(1337, note);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@SuppressWarnings("deprecation")
private void Notify(String notificationTitle, String notificationMessage,
String id, int typ) {
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(200);
Uri notif = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager
.getRingtone(getApplicationContext(), notif);
r.play();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon_notif,
getResources().getString(R.string.n_title),
System.currentTimeMillis());
notification.flags = Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent;
PendingIntent pendingIntent;
switch (typ) {
case 0:
notificationIntent = new Intent(NotifyService.this,
UlohaShowerActivity.class);
notificationIntent.putExtra(UlohaShowerActivity.ODOSLI, id);
pendingIntent = PendingIntent.getActivity(NotifyService.this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(NotifyService.this,
notificationTitle, notificationMessage, pendingIntent);
break;
case 3:
notificationIntent = new Intent(NotifyService.this,
SviatokShowerActivity.class);
notificationIntent.putExtra(SviatokShowerActivity.ODOSLI, id);
pendingIntent = PendingIntent.getActivity(NotifyService.this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(NotifyService.this,
notificationTitle, notificationMessage, pendingIntent);
break;
}
notificationManager.notify(0, notification);
}
public class NotifyBackground extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
mDbHelper = new DatabaseOp(NotifyService.this);
final boolean cyklus = true;
Thread vlakno = new Thread(new Runnable() {
@Override
public void run() {
while (cyklus) {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String sysDate = getSysDate();
String sysDate2 = getSysDate2();
String time = getSysTime();
mDbHelper.open();
Log.v("sysDate", sysDate);
Cursor cursorU = mDbHelper.fetchUlohaS(0, sysDate);
if (cursorU.getCount() > 0) {
String idU = cursorU.getString(cursorU
.getColumnIndexOrThrow(DatabaseOp.KEY_ID));
String dbDateU = cursorU.getString(cursorU
.getColumnIndexOrThrow(DatabaseOp.KEY_DATE));
String menoU = cursorU.getString(cursorU
.getColumnIndexOrThrow(DatabaseOp.KEY_NAZOV));
String mHodina = getResources().getString(
R.string.cas)
+ " "
+ cursorU.getString(cursorU
.getColumnIndexOrThrow(DatabaseOp.KEY_HODINA));
Log.v("task", dbDateU + "/" + sysDate);
if (dbDateU.equals(sysDate)) {
Notify(menoU, mHodina, idU, 0);
}
}
Cursor cursorS = mDbHelper.fetchSviatokS(3, sysDate2);
if (cursorS.getCount() > 0) {
String idS = cursorS.getString(cursorS
.getColumnIndexOrThrow(DatabaseOp.KEY_ID));
String dbDateS = cursorS.getString(cursorS
.getColumnIndexOrThrow(DatabaseOp.KEY_DATUM));
String menoS = cursorS.getString(cursorS
.getColumnIndexOrThrow(DatabaseOp.KEY_NAZOV));
if (dbDateS.equals(sysDate2)
&& time.equals("09:00")) {
Notify(menoS,
getResources().getString(
R.string.title_section4), idS,
3);
}
}
mDbHelper.close();
}
}
});
vlakno.start();
return null;
}
}
}
您可以尝试以下步骤:-
返回类型的doInBackground的线程将处理onPostExecution。现在,根据返回类型,你可以跟踪后台线程作为前台线程。
和
为什么不使用一个简单的用户定义线程?
将Service
标记为前台需要使用通知,以便用户意识到正在运行的东西没有直接的UI控件。你在Service
中实际做什么取决于你。然而,请注意,AsyncTask
是专门用于在后台(非ui/主线程)线程中执行的事情。标记为"前景"的Service
仍然在应用程序的主线程上执行其所有回调/生命周期操作。