欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android服务显式启动、隐式启动、绑定服务

程序员文章站 2022-04-02 21:21:38
一、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...

工程目录:
Android服务显式启动、隐式启动、绑定服务
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>

Android服务显式启动、隐式启动、绑定服务

本文地址:https://blog.csdn.net/weixin_43873198/article/details/108848034

相关标签: Android 过滤器