任务:unity 工具实现 游戏本地推送 ,不要网络状态
如何做:
Demo:
一,currentActivity unity 调用的Activity
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = UnityPlayer.currentActivity;
cont = this;
Log.e(TAG, "currentActivity onCreate --------------------");
//前台时。。。屏幕常亮3句话。。。mf文件也需要配置
PowerManager manager = ((PowerManager)getSystemService(POWER_SERVICE));
WakeLock wakeLock = manager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK| PowerManager.ON_AFTER_RELEASE, "ATAAW");
wakeLock.acquire();
}
public void InputParams(int clock, int type, String info) {
MyService.f=1; // 服务状态开启标记
Log.e(TAG, "currentActivity InputParams --------------------");
Intent intent = new Intent();
intent.setAction("com.lzg.tw.BIND_SERVICE");
Bundle bundle = new Bundle();
bundle.putInt("clock", clock);// 发送数据
bundle.putInt("type", type);// 发送数据
bundle.putString("info", info);// 发送数据
Log.e(TAG, "---" + clock + "---" + type + "---" + info);
intent.putExtras(bundle);
startService(intent);
Log.e(TAG, "currentActivity InputParams ---");
}
@Override
protected void onStop() {
super.onStop();
Log.e("TAG", "程序进入后台");
}
@Override
protected void onDestroy() {
Log.e("TAG", "onDestroy1111111111111111");
// MyService.f=0;
Intent newintent = new Intent(currentActivity.this,MyService.class);
newintent.setAction("com.lzg.tw.BIND_SERVICE");
startService(newintent);
super.onDestroy();
}
二:MyService 服务类
public static int f = 0;// 判断标记
// Service被创建时回调该方法。
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "MyService onCreate =========== create ===");
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
clearNotification(); //清除通知
Log.e(TAG, "MyService onCreate frist clearNotification true");
System.out.println("Service is Created");
// 启动一条线程、动态地修改count状态值,此线程用来处理系统或者其他任务所可能需要处理的耗时任务
Log.e(TAG, "new MyThread -----------");
// 初始化activityManager管理器
Log.e(TAG, "activityManager");
activityManager = (ActivityManager) this
.getSystemService(Context.ACTIVITY_SERVICE);
packageName = this.getPackageName();
System.out.println("启动服务");
new Thread(new MyThread()).start(); //开启一条线程,处理自己的复杂逻辑
}
//私有的共享文档,保存currentActivity传递过来的数据
@Override
public void onStart(Intent intent, int startId) {
Log.e(TAG, "zhu ce guangbo---------------");
MyMessage my = new MyMessage();
if (intent==null) {
return;
}
Bundle bundle = intent.getExtras();
if (bundle == null) {
return;
}
my.clcok = bundle.getInt("clock");
my.type = bundle.getInt("type");
my.info = bundle.getString("info");
list.add(my);
Log.e(TAG, "onStart-------list++++" + my.clcok + "------" + my.type
+ "****" + my.info);
Log.e(TAG, "onStart-------li------" + my.clcok + "------" + my.type
+ "//////" + my.info);
Log.e(TAG, "MyService onStart ");
SharedPreferences sharedPreferences = getSharedPreferences("MyService",
MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
clock[list.size() - 1] = list.get(list.size() - 1).clcok;
type[list.size() - 1] = list.get(list.size() - 1).type;
info[list.size() - 1] = list.get(list.size() - 1).info;
editor.putInt("clock" + list.size(), clock[list.size() - 1]);
editor.putInt("type" + list.size(), type[list.size() - 1]);
editor.putString("info" + list.size(), info[list.size() - 1]);
editor.putInt("Size", list.size());
editor.commit();
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 使程序被杀死后可以自动开启
flags = START_STICKY;// 重传Intent的值
return super.onStartCommand(intent, flags, startId);
}
// Service被关闭之前回调该方法。startService时,该步骤通常都不会实现
@Override
public void onDestroy() {
super.onDestroy();
// 通常都不会执行
Log.e(TAG, "MyService onDestroy ================");
// -----------------
System.out.println("终止服务");
System.out.println("Service is Destroyed");
}
三 MyMessage 封装信息的类,自己处理
四:广播 BootCompletedReceiver
@Override
public void onReceive(Context context, Intent intent) {
MyService.f = 0;
intent = new Intent(context,MyService.class);
intent.setAction("com.lzg.tw.BIND_SERVICE");
context.startService(intent);
}
五: mf文件配置
//权限
android:allowBackup="true" // android:persistent="true" 不可随意配置 android:name=".currentActivity" android:launchMode="singleTop"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:persistent="true"
android:process=":bootcompletedreceiver" >
android:enabled="true"
android:process=":remote" >