android 关联启动第三方应用

本文介绍了如何在Android中实现关联启动第三方应用。通过创建开机自启Service和广播接收器,设置特定Action,并授予启动权限,从而实现从一个应用启动另一个应用的功能。提供了一个开机自启服务的源码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

若是同一家公司应用,比如淘宝,支付宝,为了保持彼此存活率,通常会设置关联启动,那么该如何做呢?我们首先做个开机自启的Service:


package com.example.myapplication;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null ;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //Notification notification = new Notification(R.mipmap.ic_launcher, "myService", System.currentTimeMillis());
        Intent notificationIntent  = new Intent(this,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
        Notification notify3 = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("TickerText:" + "您有新短消息,请注意查收!")
                .setContentTitle("Notification Title")
                .setContentText("This is the notification message")
                .setContentIntent(pendingIntent).setNumber(1).build(); // 需要注意build()是在API
        // level16及之后增加的,API11可以使用getNotificatin()来替代
        notify3.flags |= Notification.FLAG_NO_CLEAR; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
       // manager.notify(1, notify3);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示
        startForeground(1, notify3);
        Log.e("myService", "onCreate");
    }
}

然后注册一个开机广播,开机权限:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
注册:

 <receiver
            android:name=".BootReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.HOME" />
            </intent-filter>
        </receiver>

然后重写一个广播接收器:

package com.example.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class BootReceiver extends BroadcastReceiver {

    static final String action_boot="android.intent.action.BOOT_COMPLETED";
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.

        //
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
           Toast.makeText(context,"开机了",Toast.LENGTH_LONG).show();
            Log.e("boot","开机了————————");
            Intent intentService = new Intent(context, MyService.class);

            context.startService(intentService);
        }
    }
}
在这里启动自己的service,这里需要在手机里打开开机自启权限。

这个应用已经做好了,那么其他应用怎么启动它呢:首先对MyService加入Action:

<service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000" >
                <action android:name="com.example.myapplication.myservice" />
            </intent-filter>
        </service>

那么其它应用启动它的时候只要拿到其包名和这个Action就可:

package com.example.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    Intent intent ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ComponentName componet = new ComponentName("com.example.myapplication.boot","com.example.myapplication.MyService");
        intent = new Intent();

        intent.setAction("com.example.myapplication.myservice");
        intent.setComponent(componet);

       startService(intent);
    }
}

然后把打开关联启动权限,这样我们就能启动第三方应用了。

源码1,开机自启demo:点击打开链接

源码2,启动第三方应用demo: 点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值