package com.example.work2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
public class MusicActivity extends AppCompatActivity {
private Button button1,button2,button3;
private MyService.Mybinder mybinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
button1=findViewById(R.id.music_button1);
button2=findViewById(R.id.music_button2);
button3=findViewById(R.id.music_button3);
Intent intent=new Intent(this,MyService.class);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopService(intent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mybinder=(MyService.Mybinder)iBinder;
mybinder.method();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mybinder=null;
}
};
bindService(intent,connection, Context.BIND_AUTO_CREATE);
}
});
}
}
package com.example.work2;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private Mybinder mybinder;
private MediaPlayer mediaPlayer;
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d("bzw","MyService onCreate...");
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.music1);
mediaPlayer.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("bzw","MyService onStartCommand...");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d("bzw","MyService onDestroy...");
mediaPlayer.stop();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
Log.d("bzw","MyService onBind...");
mybinder=new Mybinder();
return mybinder;
}
class Mybinder extends Binder{
public void method() {
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.music1);
mediaPlayer.start();
}
}
}
package com.example.work2;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity3 extends AppCompatActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
textView=findViewById(R.id.textView6);
Log.d("bzw","这是MainActivity3的生命周期:onCreate...");
Context context=this;
//Bundle bundle=new Bundle();
//bundle.putString("name2","bzw");
//bundle.putInt("age2",21);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(context,MainActivity4.class);
intent.putExtra("name","bzw");
/*intent.putExtra("usrinfo",bundle);
intent.putExtra("name","bzw");
intent.putExtra("age",21);*/
startActivityForResult(intent,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1&&resultCode==2)
{
textView.setText(data.getStringExtra("result"));
}
}
@Override
protected void onStart() {
super.onStart();
Log.d("bzw","这是MainActivity3的生命周期:onStart...");
}
@Override
protected void onPause() {
super.onPause();
Log.d("bzw","这是MainActivity3的生命周期:onPause...");
}
@Override
protected void onResume() {
super.onResume();
Log.d("bzw","这是MainActivity3的生命周期:onResume...");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("bzw","这是MainActivity3的生命周期:onRestart...");
}
@Override
protected void onStop() {
super.onStop();
Log.d("bzw","这是MainActivity3的生命周期:onStop...");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("bzw","这是MainActivity3的生命周期:onDestroy...");
}
}
package com.example.work2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity4 extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
Log.d("bzw","这是Activity4的生命周期:OnCreate...");
Intent intent4=getIntent();
String str1=intent4.getStringExtra("name");
intent4.putExtra("result",str1+"年龄是21");
setResult(2,intent4);
/*Intent intent4=getIntent();
Bundle bundle=intent4.getExtras().getBundle("usrinfo");
String str1=bundle.getString("name2");
String str2=bundle.getInt("age2")+"";
textView=findViewById(R.id.textView);
textView.setText(str1+str2);*/
}
@Override
protected void onStart() {
super.onStart();
Log.d("bzw","这是Activity4的生命周期:onStart...");
}
@Override
protected void onPause() {
super.onPause();
Log.d("bzw","这是Activity4的生命周期:onPause...");
}
@Override
protected void onResume() {
super.onResume();
Log.d("bzw","这是Activity4的生命周期:onResume...");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("bzw","这是Activity4的生命周期:onRestart...");
}
@Override
protected void onStop() {
super.onStop();
Log.d("bzw","这是Activity4的生命周期:onStop...");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("bzw","这是Activity4的生命周期:onDestroy...");
}
}
原码仓库:mytest: helloworld (gitee.com)