0

我在我的项目中使用以下代码。我仍然无法启动它

清单.xml

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name="com.android.Description.MyService"
            android:enabled="true" >
        </service>

活动

listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                startService(new Intent(Description.this, MyService.class));}

服务

public class MyService extends Service {
         private static final String TAG = "My Service Demo";
         MediaPlayer myPlayer;

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

          myPlayer = MediaPlayer.create(this, R.raw.show1);
          myPlayer.setLooping(false); // Set looping
         }

         @Override
         public void onDestroy() {
          Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
          Log.d(TAG, "onDestroy");
          myPlayer.stop();
         }

         @Override
         public void onStart(Intent intent, int startid) {
          Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
          Log.d(TAG, "onStart");
          myPlayer.start();
         }
        }

编辑 1

显现

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name="com.android.Description.MyService"
            class="com.android.Description.MyService"
            android:enabled="true" >
            <intent-filter>
                <action
                    android:name="com.android.Description.MyService"
                    android:value="com.android.Description.MyService" />
            </intent-filter>
        </service>

服务

public class MyService extends Service {
        private static final String TAG = "My Service Demo";
        MediaPlayer myPlayer;

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onCreate");

            myPlayer = MediaPlayer.create(this, R.raw.show1);
            myPlayer.setLooping(false); // Set looping
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onDestroy");
            myPlayer.stop();
        }

        @Override
        public void onStart(Intent intent, int startid) {
            super.onStart(intent, startid);
            Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
            Log.d(TAG, "onStart");
            myPlayer.start();
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
            Log.d(TAG, "onStartcommnads");
            return super.onStartCommand(intent, flags, startId);
        }    

    }

活动

listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                startService(new Intent(Description.this, MyService.class));
4

0 回答 0