ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android 新增service启动

2022-08-11 10:31:34  阅读:362  来源: 互联网

标签:Log service 启动 --- TAG intent test android public


我们现在有两个APP(ApkA,ApkB),APKB中定义了一个service,APKA启动这个service

1、首先在ApkB中定义service类

package com.example.test001;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class MyServicetest extends Service {
    private static final String TAG = MyServicetest.class.getSimpleName() + "---test---";
    IBinder mBinder = new MediaMaterialBinder1();

    public MyServicetest() {}

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "---test---onBind");
        return mBinder;
    }

    /**
     * media material Binder
     */
    public class MediaMaterialBinder1 extends Binder {
        MyServicetest getService() {
            return MyServicetest.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "---test---onCreate001");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.i(TAG, "---test---onStart");

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v(TAG, "---test---onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        super.onUnbind(intent);
        Log.i(TAG, "---test---onUnbind");
        return false;
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
        Log.i(TAG, "---test---onRebind");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "---test---onDestroy");
    }
}

 


2、在在ApkB中Manifest中定义service

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.test001">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.test001"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyServicetest"
            android:enabled="true"
            android:exported="true"></service>
    </application>

</manifest>

 

3、在ApkA中设置查询权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld">

    <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/Theme.HelloWorld">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.HelloWorld.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
<!--    <uses-permission android:name="com.permission.MediaMaterial"/>-->
<queries>
    <package android:name="com.example.test001"></package></package>
</queries>
</manifest>

 

4、在ApkA中启动Service

                Context context = getActivity();
                Intent startIntent = new Intent();
                Log.i("---test---", "start service begin");
//                startIntent.setAction("android.intent.action.START_SERVICE");
                startIntent.setComponent(new ComponentName("com.example.test001", "com.example.test001.MyServicetest"));
                try {
//                    context.startService(startIntent);
                    context.startForegroundService(startIntent);
                } catch (Exception e) {
                    Log.i("---test---", "error msg:" + e.getMessage());
                }

 

标签:Log,service,启动,---,TAG,intent,test,android,public
来源: https://www.cnblogs.com/SaraMoring/p/16575070.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有