对你们来说可能是一个简单的问题,但它是我第一次尝试创建一个应用程序关闭时应该在后台运行的服务。我的问题是:当我点击“启动服务”按钮时,服务无法启动。我没有看到任何toast,也没有看到logcat中的任何内容(也没有错误)。提前谢谢!
服务类
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
我的主要活动
public void startServiceClick(View v){
Log.d("foo", "onClick: starting srvice");
startService(new Intent(this, MyService.class));
}
public void stopServiceClick(View v){
Log.d("foo", "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
}
清单xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.johanberntsson.main"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<application
android:icon="@drawable/maps1"
android:label="@string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".LongitudeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DebugActivity" />
<activity android:name=".GoogleMapsActivity" />
<service android:enabled="true" android:name=".MyService" />
</application>
</manifest>
答案 0 :(得分:4)
this is suggestion as all else looking fine
应该在super.onCreate();
因为可能的服务已经开始并尝试停止并再次启动并检查它的吐司....
尝试
<service android:enabled="true" android:name="se.johanberntsson.servies.MyService" />
答案 1 :(得分:1)
你可能已经想到了这一点。在您的代码中,您可以覆盖多个本机函数,以便添加Toast和catlog条目。不要忘记添加超级。{无论你在呼唤什么功能}。它的作用是告诉Android设备做任何功能没有被覆盖的功能,然后做额外的东西。在您的情况下,如果您调用oncreate函数,那么计算机将进行toast并添加catlog条目,但它不会创建服务。
@Override //forget about doing whatever onCreate usually does.
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast
Log.d(TAG, "onCreate");//Add message to cat- log
//note: Service not created because function was overrided
}
但是,如果您添加super.onCreate,手机将创建该服务,然后它会执行您要求它执行的操作。
@Override //forget about doing whatever onCreate usually does.
public void onCreate() {
super.onCreate(); // do whatever onCreate usually does. Then do whatever is after me.
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast
Log.d(TAG, "onCreate");//Add message to cat- log
}
我希望这会有所帮助。
答案 2 :(得分:0)
我将您的服务添加到我的代码中,它对我来说很好。请参阅以下代码并与您的代码匹配。
活动&gt;&gt;
public class TestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt = (Button)findViewById(R.id.button11);
bt.setOnClickListener(this);
}
public void startServiceClick(View v){
Log.d("foo", "onClick: starting srvice");
startService(new Intent(this, MyService.class));
}
public void stopServiceClick(View v){
Log.d("foo", "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
}
@Override
public void onClick(View v) {
startServiceClick(v);
}
清单文件&gt;&gt;
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".MyService" />
</application>