问题呼叫服务 [英] Issue in Calling Service

查看:126
本文介绍了问题呼叫服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的服务类

 进口android.app.Service;
    进口android.content.Intent;
    进口android.os.IBinder;
    进口android.util.Log;    公共类Communicationservice延伸服务{        @覆盖
        公众的IBinder onBind(意向意图){
            // TODO自动生成方法存根
            返回null;
        }        @覆盖
        公共无效的onCreate(){
            super.onCreate();
            Log.d(I AM服务,服务创建);
        }        @覆盖
        公共无效调用onStart(意向意图,诠释startId){
            // TODO自动生成方法存根
            super.onStart(意向,startId);
        }    }这是我的主要活动
进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Intent;
进口android.util.Log;
进口android.view.Menu;公共类MainActivity延伸活动{    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        Log.d(流动,1);
        在意向=新意图(com.yal.Communication.Communicationservice);
        this.startService(在);
        // startService(新意图(MainActivity.this,Communicationservice.class));
        Log.d(流动,2);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }
}

下面是Mainfest.xml

 <应用
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>        <活性GT; ...< /活性GT;
        <服务机器人:启用=真正的机器人:名字=com.yal.Communication.Communicationservice>< /服务>
    < /用途>

错误

  16 11-02:34:17.939:D /流量(1390):1
11-02 16:34:17.939:W / ActivityManager(92):无法启动服务意向{行动= com.yal.Communication.Communicationservice}:未找到
11-02 16:34:17.939:D /流量(1390年):2


解决方案

您需要检查意向过滤器。

下面片段将帮助你。

 <服务机器人:名字=com.yal.Communication.Communicationservice>
    &所述;意图滤光器>
        <作用机器人:名字=com.yal.Communication.Communicationservice/>
    &所述; /意图滤光器>
< /服务>

然后就可以调用这个服务,如如下:

 意图serviceIntent =新意图(com.yal.Communication.Communicationservice);
startService(serviceIntent);

Here is my service class

    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;

    public class Communicationservice extends Service {

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            Log.d("I AM Service","Service Created");
        }

        @Override
        public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub
            super.onStart(intent, startId);
        }

    }

Here is my Main Activity


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Log.d("flow","1");
        Intent in = new Intent("com.yal.Communication.Communicationservice");
        this.startService(in);
        //startService(new Intent(MainActivity.this,Communicationservice.class));
        Log.d("flow","2");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Here is Mainfest.xml

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity > ...  </activity>
        <service android:enabled="true" android:name="com.yal.Communication.Communicationservice"></service>
    </application>

Error

11-02 16:34:17.939: D/flow(1390): 1
11-02 16:34:17.939: W/ActivityManager(92): Unable to start service Intent { act=com.yal.Communication.Communicationservice }: not found
11-02 16:34:17.939: D/flow(1390): 2

解决方案

you need to check Intent-Filter.

below snippet will help you.

<service android:name="com.yal.Communication.Communicationservice">
    <intent-filter>
        <action android:name="com.yal.Communication.Communicationservice" />
    </intent-filter>
</service>

Then you can call this services like follows.

Intent serviceIntent = new Intent("com.yal.Communication.Communicationservice");
startService(serviceIntent);

这篇关于问题呼叫服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆