温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android中2怎么防范Activity劫持

发布时间:2021-06-29 14:27:12 来源:亿速云 阅读:193 作者:Leah 栏目:移动开发

本篇文章给大家分享的是有关android中2怎么防范Activity劫持,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

在这一次,由于是“正当防卫”,就不再通过枚举来获取当前运行的程序了,在manifest文件中增加一个权限:

<uses-permission android:name="android.permission.GET_TASKS" />


然后启动程序的时候,启动一个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劫持,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI