若是同一家公司应用,比如淘宝,支付宝,为了保持彼此存活率,通常会设置关联启动,那么该如何做呢?我们首先做个开机自启的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: 点击打开链接