前言
我在Unity3D调用Android功能与组件(十)—BroadcastReceiver中介绍了如何使用Unity接入广播。
然而很多没有做过Android的小伙伴却表示
那我也只能
其实,广播作为Android四大组件之一,其妙用之处太多太多。你可以用来做应用自启动,也可用来做智能网络检测、电量优化大师、定位器等等。
秉着让大家少走一步弯路的原则,我还是在这里单独发一篇帖子与大家分享。感兴趣的小伙伴还是建议多看看Unity3D调用Android功能与组件(十)—BroadcastReceiver这篇,因为有更高的*度任你发挥。
正文:
1.首先申请相应权限
在AndroidMenifest.xml中添加下列权限,可根据自己需要酌情增减
< uses-permission android:name = "android.permission.RECEIVE_BOOT_COMPLETED" />
< uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
< uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />
< uses-permission android:name = "android.permission.READ_PHONE_STATE" />
< uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE" />
< uses-permission android:name = "android.permission.BIND_ACCESSIBILITY_SERVICE" />
< uses-permission android:name = "android.permission.BIND_SCREENING_SERVICE" />
< uses-permission android:name = "android.permission.RECEIVE_USER_PRESENT" />
|
温馨提示:请妥善使用权限,坚决不申请用不到的权限
2.添加广播接收
< receiver
android:name = ".SelfWakeBroadcastReceiver"
android:process = ":remote" >
< intent-filter android:priority = "90000" >
< action android:name = "android.intent.action.BOOT_COMPLETED" />
< action android:name = "android.net.conn.CONNECTIVITY_CHANGE" />
< action android:name = "android.intent.action.PHONE_STATE" />
< action android:name = "android.intent.action.DATE_CHANGED" />
< action android:name = "android.intent.action.ACTION_POWER_CONNECTED" />
< action android:name = "android.intent.action.SCREEN_OFF" />
< action android:name = "android.intent.action.SCREEN_ON" />
< category android:name = "android.intent.category.HOME" />
< action android:name = "android.intent.action.USER_PRESENT" />
</ intent-filter >
</ receiver >
|
在AndroidMenifest.xml中把上面的代码加到Application标签里面。
3.Java源码及打包
package com.unity3d.player;
import java.util.List;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.util.Log;
/** * Created by User on 2016/11/19.
*/
public class SelfWakeBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v( "SelfWakeBroadcast" , "has Receive a broadcast" );
RunApp(context, "com.hhu.virtualhair" ); //TODO 换成自己的包名
}
public void registerScreenActionReceiver(Context mContext) {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_USER_PRESENT);
mContext.registerReceiver(SelfWakeBroadcastReceiver. this , filter);
}
private void RunApp(Context context,String packageName) {
PackageInfo pi;
try {
pi = context.getPackageManager().getPackageInfo(packageName, 0 );
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null );
resolveIntent.setPackage(pi.packageName);
PackageManager pManager = context.getPackageManager();
List<ResolveInfo> apps = pManager.queryIntentActivities(
resolveIntent, 0 );
ResolveInfo ri = apps.iterator().next();
if (ri != null ) {
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName(ri.activityInfo.packageName, className);
intent.setComponent(cn);
context.startActivity(intent);
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
} |
把上述Java代码中的//TODO 换成自己的包名处的报名替换为自己应用的报名;
将上述代码打包成.jar;
把打包好的jar文件放到Unity的/Assets/Plugins/Android/文件夹下。
4.大功告成!
发布你的软件或游戏,享受自启的快感!
--------------------------------2018.09.06--更新----------------------
本帖子内容实际上是2016年的一个回答,由于时间比较久远,随着Android系统的升级,可能某些小伙伴使用本帖子提供的教程会出问题,MemoryC也没来得及去测试与更正。
最近交流群里三位小伙伴一起探讨了一个方案,我把他们得出的教程粘在这里,有需要的码友可参考下。
Unity3D调用Android功能与组件(10.1):应用自启动
http://www.manew.com/thread-139699-1-1.html
(出处: -【游戏蛮牛】-游戏出海,ar增强现实,虚拟现实,unity3d,unity3d教程下载首选u3d,unity3d官网)