Android 呼叫服务中的问题
这是我的服务课Android 呼叫服务中的问题,android,Android,这是我的服务课 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 in
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;
}
}
下面是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>
您需要检查意图过滤器 下面的代码片段将帮助您
<service android:name="com.yal.Communication.Communicationservice">
<intent-filter>
<action android:name="com.yal.Communication.Communicationservice" />
</intent-filter>
</service>
构造函数
公共意图(字符串操作)
只能用于指定操作请参见以下java文档:
public Intent (String action)
Create an intent with a given action. All other fields (data, type, class) are null. Note that the action must be in a namespace because Intents are used globally in the system -- for example the system VIEW action is android.intent.action.VIEW; an application's custom action would be something like com.google.app.myapp.CUSTOM_ACTION.
Parameters
action The Intent action, such as ACTION_VIEW.
对您的创建意图进行以下更改:
Intent in = new Intent(this,com.yal.Communication.Communicationservice.class);
或在me清单文件中指定操作:
<service android:name="com.yal.Communication.Communicationservice">
<intent-filter>
<action android:name="com.yal.Communication.Communicationservice" />
</intent-filter>
</service>
您确定com.yal.Communication中有通信服务吗?因为其他的东西看起来很好。谢谢,它起作用了。。为什么我需要添加意图过滤器?意图过滤器是一种机制,您可以通过它指定在特定操作中应调用哪些活动/服务/广播接收器。
Intent in = new Intent(this,com.yal.Communication.Communicationservice.class);
<service android:name="com.yal.Communication.Communicationservice">
<intent-filter>
<action android:name="com.yal.Communication.Communicationservice" />
</intent-filter>
</service>