H5+应用 启动 另一安卓应用的service

安卓程序:

在manifast.xml中定义service

        <service
            android:name=".DataService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.h5appdataprovider.dataService" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </service>

MainActivity.java的onCreate中添加:
 

        // 创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel("channel_androidToH5_1", "AndroidToH5Channel", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }

DataService.java

public class DataService extends Service {

    public DataService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 获取传递的数据
        String name = intent.getStringExtra("name");
        Toast.makeText(getApplicationContext(), "Data Service Started: get name = " + name, Toast.LENGTH_SHORT).show();

        // 创建通知
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder = new Notification.Builder(this, "channel_androidToH5_1");
        } else {
            builder = new Notification.Builder(this);
        }
        builder.setContentTitle("Data Service")
                .setContentText("Data Service Started");
        startForeground(1, builder.build());

        return super.onStartCommand(intent, flags, startId);
    }

}

H5启动服务:

let context = plus.android.runtimeMainActivity();
// 通过反射获取Android的Intent对象
let Intent = plus.android.importClass("android.content.Intent");
// 创建 intent
let _intent = new Intent();
// 要传递的数据
_intent.putExtra("name", "张三");
_intent.setAction("com.example.h5appdataprovider.dataService");
_intent.setPackage("com.example.h5appdataprovider");
console.log(JSON.stringify(_intent));
plus.android.requestPermissions(
    ["android.permission.QUERY_ALL_PACKAGES"],
    function(resultObj) {
        console.log("申请权限成功!");
        //启动Activity
        context.startForegroundService(_intent);
    },
    function(resultObj) {
        console.log("申请权限失败!");
    }
);

注意setAction要与intent-filter中的内容一致哦

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值