我正在尝试开始一项梦想服务。目前,这是我的代码:
@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();
}
}我无法从另一个活动中启动dream服务。这是我使用的:
Intent tempLock = new Intent(MainActivity.this, DreamLockService.class);
//DreamLockService test = new DreamLockService();
startService(tempLock);我不明白为什么它不起作用。如何从另一个活动启动梦想服务?
发布于 2015-01-24 13:24:56
若要从我们自己的应用程序启动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();
} 要使用此功能,您的应用程序应该是系统应用程序,并且应该在清单文件中具有dream权限,并在设置中启用dream设置。
我试过了,它起作用了。
发布于 2013-05-14 23:49:03
您是否将其包含在清单中,并拥有与您的操作相匹配的<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;
}https://stackoverflow.com/questions/16547307
复制相似问题