请问,我如何从thread
doInBackground AsyncTask
foreground
获取service
?我需要thread
永远不会被杀死,Notify notification
与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;
}
}
}
无法显示,但{{1}}始终处于启用状态。
{{1}}
答案 0 :(得分:2)
您可以尝试以下步骤: -
线程的doInBackground的返回类型将处理onPostExecution。现在,基于返回类型,您可以将后台线程作为前台线程进行跟踪。
和
为什么你没有使用简单的用户定义线程?
答案 1 :(得分:0)
将Service
标记为前台需要使用通知,以便用户知道正在运行的内容没有直接的UI控件。您在Service
中实际执行的操作取决于您。但请注意,AsyncTask
专门用于在后台(非UI /主)线程中执行操作。标记为“前景”的Service
仍然在应用程序的主线程上执行其所有回调/生命周期操作。