1

我正在尝试开始一项梦想服务。目前,这是我的代码:

@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);

我不明白为什么它不起作用。如何从另一项活动开始梦想服务?

4

2 回答 2

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();
    } 

要使用它,您的应用程序应该是系统应用程序,并且应该在 Manifest 文件中具有梦想权限,并在设置中启用梦想设置。

我试过了,它正在工作。

于 2015-01-24T05:24:55.533 回答
-3

您是否将其包含在您的清单中并有一个<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;
}
于 2013-05-14T15:49:03.390 回答