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