[英]How to create and start a dream service atomatically android
我正在嘗試開始一項夢想中的服務。 目前,這是我的代碼:
@SuppressLint("NewApi")
public class DreamLockService extends DreamService {
private static final String TAG = "DreamLockService";
public Utility utilObj = new Utility();
//private Button btnExit;
private Button btnlogin;
private EditText lgPass;
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
// Exit dream upon user touch
setInteractive(true);
// Hide system UI
setFullscreen(true);
// Set the dream layout
setContentView(R.layout.lockservice);
//setClickListener();
Toast.makeText(this, "Lock Service Created", Toast.LENGTH_LONG).show();
}
//Use this for initial setup, such as calling setContentView().
@Override
public void onDreamingStarted() {
super.onDreamingStarted();
// Exit dream upon user touch
setInteractive(true);
// Hide system UI
setFullscreen(true);
// Set the dream layout
setContentView(R.layout.lockservice);
Toast.makeText(this, "Lock Service Created", Toast.LENGTH_LONG).show();
}
//Your dream has started, so you should begin animations or other behaviors here.
public void onDreamingStopped()
{
super.onDreamingStopped();
}
//Use this to stop the things you started in onDreamingStarted().
public void onDetachedFromWindow()
{
super.onDetachedFromWindow();
}
}
我無法從另一個活動啟動夢想服務。 這是我使用的:
Intent tempLock = new Intent(MainActivity.this, DreamLockService.class);
//DreamLockService test = new DreamLockService();
startService(tempLock);
我不明白為什么它不起作用。 如何從另一個活動啟動夢想服務?
要從我們自己的應用程序啟動 Dream 服務,請嘗試此操作。
IBinder binder = ServiceManager.getService("dreams");
Parcel data = Parcel.obtain();
data.writeInterfaceToken("android.service.dreams.IDreamManager");
Parcel reply = Parcel.obtain();
try {
if (binder != null)
binder.transact(1, data,reply, Binder.FLAG_ONEWAY);
else
Log.e("TAG", "dreams service is not running");
} catch (RemoteException e) {
e.printStackTrace();
}
要使用它,您的應用程序應該是系統應用程序,並且應該在清單文件中具有夢想權限並在設置中啟用夢想設置。
我試過這個,它正在工作。
使用AtomicBoolean或任何其他Atomic數據結構以原子方式在Java中啟動白日夢服務。
您是否將它包含在清單中並有一個與您的操作相匹配的<intent-filter>
?
如果沒問題,請嘗試:
startService(new Intent(this, DreamLockService.class));
一個優秀的服務教程: http : //www.vogella.com/articles/AndroidServices/article.html#scheduleservice_startauto
更新:
由於您似乎不確定您的服務是否正在運行,您可以使用此處找到的解決方案:
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.