android中2怎么防范Activity劫持
本篇文章给大家分享的是有关android中2怎么防范Activity劫持,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
遂宁ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
在这一次,由于是“正当防卫”,就不再通过枚举来获取当前运行的程序了,在manifest文件中增加一个权限:
然后启动程序的时候,启动一个Service,在Service中启动一个浮动窗口,并周期性检测当前运行的是哪一个程序,然后显示在浮动窗口中。
程序截图如下:
其中Service代码如下:
/* * @(#)AntiService.java Project:ActivityHijackingDemo * Date:2012-9-13 * * Copyright (c) 2011 CFuture09, Institute of Software, * Guangdong Ocean University, Zhanjiang, GuangDong, China. * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sinaapp.msdxblog.antihijacking.service; import android.app.ActivityManager; import android.app.Notification; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.util.Log; import com.sinaapp.msdxblog.androidkit.thread.HandlerFactory; import com.sinaapp.msdxblog.antihijacking.AntiConstants; import com.sinaapp.msdxblog.antihijacking.view.AntiView; /** * @author Geek_Soledad (66704238@51uc.com) */ public class AntiService extends Service { private boolean shouldLoop = false; private Handler handler; private ActivityManager am; private PackageManager pm; private Handler mainHandler; private AntiView mAntiView; private int circle = 2000; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); startForeground(19901008, new Notification()); if (intent != null) { circle = intent.getIntExtra(AntiConstants.CIRCLE, 2000); } Log.i("circle", circle + "ms"); if (true == shouldLoop) { return; } mAntiView = new AntiView(this); mainHandler = new Handler() { public void handleMessage(Message msg) { String name = msg.getData().getString("name"); mAntiView.setText(name); }; }; pm = getPackageManager(); shouldLoop = true; am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); handler = new Handler( HandlerFactory.getHandlerLooperInOtherThread("anti")) { @Override public void handleMessage(Message msg) { super.handleMessage(msg); String packageName = am.getRunningTasks(1).get(0).topActivity .getPackageName(); try { String progressName = pm.getApplicationLabel( pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA)).toString(); updateText(progressName); } catch (NameNotFoundException e) { e.printStackTrace(); } if (shouldLoop) { handler.sendEmptyMessageDelayed(0, circle); } } }; handler.sendEmptyMessage(0); } private void updateText(String name) { Message message = new Message(); Bundle data = new Bundle(); data.putString("name", name); message.setData(data); mainHandler.sendMessage(message); } @Override public void onDestroy() { shouldLoop = false; mAntiView.remove(); super.onDestroy(); } }
以上就是android中2怎么防范Activity劫持,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
新闻标题:android中2怎么防范Activity劫持
转载来源:http://azwzsj.com/article/ppcpdc.html