【问题标题】:Unable to start service Intent, U=0: not found无法启动服务 Intent,U=0:未找到
【发布时间】:2017-09-12 05:50:20
【问题描述】:

我在这个问题上苦苦挣扎了两天。 我有两个应用程序并尝试通过 Messenger IPC 进行通信,但它甚至无法绑定到服务器应用程序的服务。

--- 服务器端

    <service android:name="com.example.RemoteService" android:exported="true">
    </service>
</application>

--客户端

@Override
protected void onCreate(Bundle savedInstanceState) {
             :
     this.connection = new RemoteServiceConnection();

@Override
public void onStart() {
           :
     Intent intent = new Intent();
    intent.setClassName("com.example.client", "com.example.RemoteService");
    //Intent i = new Intent("com.example.RemoteService");
    //i.setPackage(this.getPackageName());
    boolean ret = bindService(intent , this.connection, Context.BIND_AUTO_CREATE);

ret 始终为 false 并始终接收系统消息 W/ActivityManagerService:无法启动服务 Intent { act=com.example.RemoteService pkg=com.example.client } U=0:未找到

似乎一切都可以实施。我觉得这是底层问题。请帮忙。

谢谢

这里是服务类

public class RemoteService extends Service
{
    private Messenger messenger; //receives remote invocations
    @Override
    public IBinder onBind(Intent intent)
    {
        if(this.messenger == null)
        {
            synchronized(RemoteService.class)
            {
                if(this.messenger == null)
                {
                    this.messenger = new Messenger(new IncomingHandler());
                }
            }
        }
        //Return the proper IBinder instance
        return this.messenger.getBinder();
    }

    private class IncomingHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg)
        {
            System.out.println("*****************************************");
            System.out.println("Remote Service successfully invoked!!!!!!");
            System.out.println("*****************************************");

            int what = msg.what;

            Toast.makeText(RemoteService.this.getApplicationContext(), "Remote Service invoked-("+what+")", Toast.LENGTH_LONG).show();

            //Setup the reply message
            Message message = Message.obtain(null, 2, 0, 0);
            try
            {
                //make the RPC invocation
                Messenger replyTo = msg.replyTo;
                replyTo.send(message);
            }
            catch(RemoteException rme)
            {
                //Show an Error Message
                Toast.makeText(RemoteService.this, "Invocation Failed!!", Toast.LENGTH_LONG).show();
            }
        }
    }
}

【问题讨论】:

  • 你的包定义正确吗?如果您的服务在包com.example.client 中,它应该是com.example.client.RemoteService(类似地,如果您的服务在包com.example 中定义,则包和服务字符串应该是com.examplecom.example.RemoteService

标签: android service ipc messenger


【解决方案1】:

尝试像这样启动您的服务

startService(new Intent(this, YourService.class));

不要忘记在清单中添加您的服务,

<application>

    <service
        android:name=".MyService"
         >
    </service>

</application>

【讨论】:

  • 它仍然有错误“W/ActivityManagerService: Unable to start service Intent { act=com.example.RemoteService pkg=com.example.client } U=0: not found”
【解决方案2】:

setClassName以包名和类名为参数:

intent.setClassName("com.example.client", "com.example.RemoteService");

如果你的包名是 com.example.client 那么类名应该是 RemoteServicecom.example.client.RemoteService

您还可以检查包是否出现在: /data/data/com.example....

这个错误只是因为找不到包/类而发生

【讨论】:

    【解决方案3】:

    有时,如果您在代码中找不到任何错误,您可以尝试重新启动设备。可以解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多