美文网首页
服务Service

服务Service

作者: Trap_余生 | 来源:发表于2017-04-18 15:15 被阅读4次
    服务和Activity不交互
    public class MyService extends Service {
    public static final String TAG="MyService";
    private MediaPlayer mediaPlayer;
    
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind: "+System.currentTimeMillis());
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mediaPlayer.start();
        Log.i(TAG, "onStart: "+System.currentTimeMillis());
        return super.onStartCommand(intent, flags, startId);
    }
    
    @Override
    public void onCreate() {
        mediaPlayer = MediaPlayer.create(this, Uri.fromFile(new File("/sdcard/remix.mp3")) );
        mediaPlayer.setLooping(false);
        Log.i(TAG, "onCreate: "+System.currentTimeMillis());
        Log.i(TAG, "onCreate: "+Thread.currentThread().toString());
        Log.i(TAG, "onCreate: "+Thread.currentThread().getId());
        Log.e(TAG, "onCreate: "+ Process.myPid());
    }
    
    @Override
    public void onDestroy() {
        mediaPlayer.stop();
        Log.i(TAG, "onDestroy: "+ System.currentTimeMillis());
      }
    }
    
     @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start:
                startService(new Intent("com.iflytek.MYSERVICE"));
                //startService(new Intent(this,MyService.class));
                Log.i(TAG, "onClick: start Service");
                break;
            case R.id.stop:
                stopService(new Intent("com.iflytek.MYSERVICE"));
                Log.i(TAG, "onClick: stop Service");
                break;
            default:
                break;
        }
    }
    
    服务和Activity交互

    相关文章

      网友评论

          本文标题:服务Service

          本文链接:https://www.haomeiwen.com/subject/foofzttx.html