1

对你们来说可能是一个简单的问题,但这是我第一次尝试创建一个应该在我的应用程序关闭时在后台运行的服务。我的问题是:单击“启动服务”按钮时服务没有启动。我没有看到任何吐司,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>
4

3 回答 3

4
this is suggestion as all else looking fine 

应该在每个函数中调用 supersuper.onCreate();

可能服务已经启动并尝试停止并重新启动并检查它是否出现吐司......

尝试

<service android:enabled="true" android:name="se.johanberntsson.servies.MyService" />
于 2012-06-30T19:07:26.387 回答
1

你可能已经想通了。在您的代码中,您覆盖了几个本机函数,因此您可以添加一个 toast 和一个 catlog 条目。不要忘记添加 super.{无论您调用什么函数}。它的作用是告诉 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

    }

我希望这有帮助。

于 2013-12-27T16:11:20.470 回答
0

我将您的服务添加到我的代码中,它对我来说工作正常。请参阅下面的代码并与您的匹配。

活动>>

 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);   

}

清单文件>>

   <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>

于 2012-06-30T19:46:59.460 回答