Android服务显式启动、隐式启动、绑定服务
程序员文章站
2024-01-09 21:47:04
一、Service显式启动工程目录:MainActivitypackage com.example.d1;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;publ...
工程目录:
app-MainActivity
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private String otherPackage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
otherPackage="com.example.app1";
break;
case R.id.button2:
otherPackage="com.example.app2";
break;
case R.id.button3:
otherPackage="com.example.app3";
break;
}
Intent intent=getPackageManager().getLaunchIntentForPackage(otherPackage);
if(intent==null){
Toast.makeText(this, "not this app", Toast.LENGTH_SHORT).show();
}else{
startActivity(intent);
}
}
}
app1-MainActivity
package com.example.app1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent;
switch (v.getId()){
case R.id.button:
Toast.makeText(this, "play music", Toast.LENGTH_SHORT).show();
intent=new Intent(MainActivity.this,MyService.class);
startService(intent);
findViewById(R.id.button).setEnabled(false);
findViewById(R.id.button2).setEnabled(true);
break;
case R.id.button2:
Toast.makeText(this, "stop music", Toast.LENGTH_SHORT).show();
intent=new Intent(MainActivity.this,MyService.class);
stopService(intent);
findViewById(R.id.button).setEnabled(true);
findViewById(R.id.button2).setEnabled(false);
break;
}
}
}
MyService
package com.example.app1;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MyService extends Service {
MediaPlayer mp;
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
mp=MediaPlayer.create(getApplicationContext(),R.raw.cc);
mp.start();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
super.onDestroy();
mp.stop();
}
}
app2-MainActivity
package com.example.app2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent;
switch (v.getId()){
case R.id.button:
Toast.makeText(this, "play music", Toast.LENGTH_SHORT).show();
/*intent=new Intent(MainActivity.this,MyService.class);*/
intent=new Intent("com.zlm.MAS");
intent.setPackage("com.example.app2");
startService(intent);
findViewById(R.id.button).setEnabled(false);
findViewById(R.id.button2).setEnabled(true);
break;
case R.id.button2:
Toast.makeText(this, "stop music", Toast.LENGTH_SHORT).show();
/*intent=new Intent(MainActivity.this,MyService.class);*/
intent=new Intent("com.zlm.MAS");
intent.setPackage("com.example.app2");
stopService(intent);
findViewById(R.id.button).setEnabled(true);
findViewById(R.id.button2).setEnabled(false);
break;
}
}
}
MyService
package com.example.app2;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MyService extends Service {
MediaPlayer mp;
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
mp=MediaPlayer.create(getApplicationContext(),R.raw.cc);
mp.start();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onDestroy() {
super.onDestroy();
mp.stop();
}
}
Manifestes
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="com.zlm.MAS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
</application>
</manifest>
app3-MainActivity
package com.example.app3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyService.PlayBinder playBinder= (MyService.PlayBinder) service;
playBinder.MyMethod();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
Intent intent=new Intent("com.zlm.MPS");
intent.setPackage("com.example.app3");
bindService(intent,conn,BIND_AUTO_CREATE);
}
}
MyService
package com.example.app3;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
private MediaPlayer mp;
public class PlayBinder extends Binder {
public void MyMethod(){
mp=MediaPlayer.create(getApplicationContext(),R.raw.cc);
mp.start();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
//throw new UnsupportedOperationException("Not yet implemented");
return new PlayBinder();
}
@Override
public void onDestroy(){
if(mp!=null){
mp.stop();
mp.release();
}
super.onDestroy();
}
}
Manifestes
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app3">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.zlm.MPS"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
本文地址:https://blog.csdn.net/weixin_43873198/article/details/108848034
推荐阅读
-
Android服务显式启动、隐式启动、绑定服务
-
Android-----Intent中通过startActivity(Intent intent )显式启动新的Activity
-
Android显式启动与隐式启动Activity的区别介绍
-
android开发艺术探索 第一章 intentFilter的匹配规则 隐式启动匹配规则
-
带你手写基于 Spring 的可插拔式 RPC 框架(四)代理类的注入与服务启动 JavaRPC手写框架开源
-
Android-----Intent中通过startActivity(Intent intent )显式启动新的Activity
-
Android 隐式启动Activity方式之IntentFilter匹配规则
-
Android显式启动与隐式启动Activity的区别介绍
-
android开发艺术探索 第一章 intentFilter的匹配规则 隐式启动匹配规则
-
Android服务显式启动、隐式启动、绑定服务