我正在尝试开始一项梦想服务。目前,这是我的代码:
@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);
我不明白为什么它不起作用。如何从另一项活动开始梦想服务?
答案 0 :(得分:2)
要从我们自己的应用启动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();
}
要使用此功能,您的应用应该是系统应用,并且应该在清单文件中具有梦想权限,并在设置中启用梦想设置。
我试过这个并且它正在运行。
答案 1 :(得分:0)
使用AtomicBoolean或任何其他Atomic数据结构以原子方式在Java中启动白日梦服务。
答案 2 :(得分:-1)
您是否已将其包含在清单中并且<intent-filter>
符合您的操作?
如果没问题,请尝试:
startService(new Intent(this, DreamLockService.class));
优秀的服务教程:http://www.vogella.com/articles/AndroidServices/article.html#scheduleservice_startauto
更新:
由于您似乎不确定您的服务是否正在运行,因此您可以使用此解决方案here:
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;
}