tôi thấy các lỗi sau đây trong DDMS khi cố gắng sử dụng một CheckBox trên MyActivity tôi" hoạt động để bắt đầu một dịch vụ gọi là 'MyService':"Không thể bắt đầu dịch vụ Ý định" lỗi khi khởi chạy dịch vụ từ một hoạt động trong Android
W/ActivityManager( 73): Unable to start service Intent { cmp=com.example.android.myprogram/.MyService }: not found
tôi đã sử dụng các hướng dẫn http://developer.android.com/resources/tutorials/views/hello-formstuff.html và thêm mã được cung cấp đến hết phương pháp của tôi onCreate(). tôi có các lớp học được quy định riêng trong MyActivity.java và MyService.java.
package com.example.android.myprogram;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
public class MyActivity extends Activity {
private static final String TAG = "MyActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks, depending on whether it's now checked
if (((CheckBox) v).isChecked()) {
// TODO: Add code to START the service
Log.d(TAG, "startService from checkbox");
startService(new Intent(MyActivity.this, MyService.class));
} else {
// TODO: Add code to STOP the service
Log.d(TAG, "stopService from checkbox");
stopService(new Intent(MyActivity.this, MyService.class));
}
}
});
}
}
file manifest của tôi không có sau đây tôi cũng đã thử đầy đủ không gian tên, tên viết tắt, sử dụng bộ lọc ý định cho mỗi tìm kiếm khác, v.v. Tôi không nói điều gì là đúng. Tôi chỉ để nó ở điểm dừng.
<service android:name=".MyService">
<intent-filter><action android:name="com.example.android.myprogram.MyService"></action>
</intent-filter>
</service>
Và cuối cùng, dịch vụ của tôi mà tôi đã quyết định để phá vỡ xuống để nó trần tối thiểu:
package com.example.android.myprogram;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
//code to execute when the service is first created
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy");
//code to execute when the service is shutting down
}
@Override
public void onStart(Intent intent, int startid) {
Log.d(TAG, "onStart");
//code to execute when the service is starting up
}
}
Tôi rất, rất, rất mới để Java/lập trình Android và lập trình nói chung (nhưng học tập) vì vậy tôi chắc chắn đây là lỗi của người dùng và có lẽ là ý thức chung đối với mọi người khác. Bất kỳ đề xuất nào cũng sẽ rất tuyệt.
bạn có thể dán mã của dịch vụ? – Cristian
Cristian, cảm ơn bạn đã trả lời. Mã đã được thêm. Câu hỏi: Tôi có thể sử dụng điều này và nhận được kết quả thành công không? http://android.kgmoney.net/2010/05/08/creating-a-simple-android-service-for-background-processing/. Nó không hoạt động. – capitalf