20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程

简介: 场景:实现安装一个apk应用程序的过程。界面如下:编写如下应用,应用结构如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android

  1. 场景:实现安装一个apk应用程序的过程。界面如下:

  1. 编写如下应用,应用结构如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

   

    <EditText

        android:hint="请输入安装apk文件的路径"

         android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/et_path"/>

   

    <Button

        android:onClick="click"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="安装"/>

   

</RelativeLayout>

  1. MainActivity的内容如下:

package com.itheima.apkinstaller;

 

import java.io.File;

 

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

 

public class MainActivity extends Activity {

    private EditText et_path;

   

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       et_path = (EditText) findViewById(R.id.et_path);

    }

 

    public void click(View view) {

       String path = et_path.getText().toString().trim();

//      <intent-filter>

//      <action android:name="android.intent.action.VIEW" />

//      <category android:name="android.intent.category.DEFAULT" />

//      <data android:scheme="content" />

//      <data android:scheme="file" />

//      <data android:mimeType="application/vnd.android.package-archive" />

//      </intent-filter>

      

       Intent intent = new Intent();

       intent.setAction("android.intent.action.VIEW");

       intent.addCategory("android.intent.category.DEFAULT");

       intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");

       startActivity(intent);

    }

}

  1. Android清单文件内容如下:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.itheima.apkinstaller"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="19" />

 

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.itheima.apkinstaller.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

1 场景:通过WebView加载一个页面,程序运行后的界面如下(注意:下面的页面是通过WebView通过load的方式加载进去的):

程序案例结构如下:

2  编写activity_main.xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <!-- 嵌入的浏览器 -->

    <WebView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:id="@+id/wv"/>

   

</RelativeLayout>

3 Android的清单文件:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.itheima.htmlui"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="19" />

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

   

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.itheima.htmlui.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

1 Android通知,界面效果:

点击点击显示通知,发现上方出现了一个红色小人的通知

点击新版点击显示通知,将通知滑动显示查看,效果如下:

程序案例结构如下:

2 编写布局文件:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

 

    <Button

        android:onClick="click"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="点击显示通知" />

    <Button

        android:onClick="click2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:text="新版点击显示通知" />

   

</RelativeLayout>

3 编写MainActivity,代码如下:

package com.itheima.notification;

 

import android.annotation.SuppressLint;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.view.View;

 

public class MainActivity extends ActionBarActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

    }

 

    public void click(View view){

       NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

       Notification notification = new Notification(R.drawable.notification,

           "我是一个通知", System.currentTimeMillis());

       notification.flags = Notification.FLAG_AUTO_CANCEL;

       Intent intent = new Intent();

       intent.setAction(Intent.ACTION_CALL);

       intent.setData(Uri.parse("tel:10086"));

       //延期的意图

       PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);

       //设置点击事件的类型

       notification.setLatestEventInfo(this, "我是标题", "我是内容", contentIntent);

       nm.notify(0, notification);

    }

   

    /**

     * 新版本的notification

     * @param view

     */

    @SuppressLint("NewApi")

    public void click2(View view){

        Notification noti = new Notification.Builder(this)

         .setContentTitle("我是标题")  //这些都是Android版本11版本开始的

         .setContentText("我是内容")

         .setSmallIcon(R.drawable.notification)

         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))

         .build();

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        nm.notify(0, noti);

    }

}

Android清单文件:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.itheima.notification"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="19" />

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

   

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.itheima.notification.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

=============================================================================

1        自杀代码:杀死进程的代码如下:

package com.itheima.exit;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.os.Bundle;

 

public class MainActivity extends Activity {

   

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

    }

   

    @Override

    public void onBackPressed() {

       AlertDialog.Builder builder = new Builder(this);

       builder.setTitle("提醒");

       builder.setMessage("确定退出当前应用程序吗?");

       builder.setPositiveButton("立刻退出", new OnClickListener() {

          

           public void onClick(DialogInterface dialog, int which) {

              finish();//关闭当前的activity

              //把自己的进程杀死

              //自杀的方法

               android.os.Process.killProcess(android.os.Process.myPid());

           }

       });

       builder.setNegativeButton("取消", null);

       builder.show();

    }

}

程序退出先的效果(在Devices中的效果):

退出后:

 

 

1 杀死进程,场景:杀死空进程,后台进程,他杀(也就是说杀不死自己),界面效果:

程序案例代码结构如下:

2 编写activity_main.xml布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

   

    <EditText

        android:hint="请输入要杀死的进程包名"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:id="@+id/et_packname"/>

   

    <Button

        android:onClick="click"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:text="杀死进程"/>

</RelativeLayout>

3 MainActivity的代码如下:

package com.itheima.killother;

 

import android.app.Activity;

import android.app.ActivityManager;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

 

public class MainActivity extends Activity {

    private ActivityManager am;//相当于进程管理器

    private EditText et_packname;

 

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

      

       //所有程序的开启都是由ActivityActivityManager来管理的

       am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

        et_packname = (EditText) findViewById(R.id.et_packname);

    }

   

    public void click(View view) {

       String packname = et_packname.getText().toString().trim();

       //注意要杀死别的进程需要加上权限

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

       //这里中方式只是杀死别人,注意:手机里面有些进程是杀死不掉的,比如电话进程

       am.killBackgroundProcesses(packname);

    }

}

程序运行结果:

注意:如果有时候提示要重启adb,可以通过下面的方式:

下面以杀死上面的com.android.music为例:

点击杀死进程前:

点击杀死进程之后的效果:

 

 

 

 

目录
相关文章
|
13天前
|
移动开发 安全 Java
Android历史版本与APK文件结构
通过以上内容,您可以全面了解Android的历史版本及其主要特性,同时掌握APK文件的结构和各部分的作用。这些知识对于理解Android应用的开发和发布过程非常重要,也有助于在实际开发中进行高效的应用管理和优化。希望这些内容对您的学习和工作有所帮助。
149 83
|
24天前
|
前端开发 Java 编译器
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
76 36
当flutter react native 等混开框架-并且用vscode-idea等编译器无法打包apk,打包安卓不成功怎么办-直接用android studio如何打包安卓apk -重要-优雅草卓伊凡
|
1月前
|
Dart 前端开发 Android开发
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
41 1
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
|
6月前
|
网络协议 Linux
Linux查看端口监听情况,以及Linux查看某个端口对应的进程号和程序
Linux查看端口监听情况,以及Linux查看某个端口对应的进程号和程序
804 2
|
6月前
|
Linux Python
linux上根据运行程序的进程号,查看程序所在的绝对路径。linux查看进程启动的时间
linux上根据运行程序的进程号,查看程序所在的绝对路径。linux查看进程启动的时间
117 2
|
6月前
|
安全 Java Android开发
【Android P】OTA升级包定制,移除不需要更新的分区,重新打包签名
如何解压OTA升级包、编辑升级包内容(例如移除不需要更新的分区)、重新打包、签名以及验证OTA文件的过程。
538 2
【Android P】OTA升级包定制,移除不需要更新的分区,重新打包签名
|
5月前
|
Java Android开发 数据安全/隐私保护
Android中多进程通信有几种方式?需要注意哪些问题?
本文介绍了Android中的多进程通信(IPC),探讨了IPC的重要性及其实现方式,如Intent、Binder、AIDL等,并通过一个使用Binder机制的示例详细说明了其实现过程。
497 4
|
5月前
|
Python
惊!Python进程间通信IPC,让你的程序秒变社交达人,信息畅通无阻
【9月更文挑战第13天】在编程的世界中,进程间通信(IPC)如同一场精彩的社交舞会,每个进程通过优雅的IPC机制交换信息,协同工作。本文将带你探索Python中的IPC奥秘,了解它是如何让程序实现无缝信息交流的。IPC如同隐形桥梁,连接各进程,使其跨越边界自由沟通。Python提供了多种IPC机制,如管道、队列、共享内存及套接字,适用于不同场景。通过一个简单的队列示例,我们将展示如何使用`multiprocessing.Queue`实现进程间通信,使程序如同社交达人般高效互动。掌握IPC,让你的程序在编程舞台上大放异彩。
44 3
|
4月前
|
安全 API C#
C# 如何让程序后台进程不被Windows任务管理器强制结束
C# 如何让程序后台进程不被Windows任务管理器强制结束
126 0
|
6月前
|
数据采集 监控 API
如何监控一个程序的运行情况,然后视情况将进程杀死并重启
这篇文章介绍了如何使用Python的psutil和subprocess库监控程序运行情况,并在程序异常时自动重启,包括多进程通信和使用日志文件进行断点重续的方法。

热门文章

最新文章

  • 1
    【08】flutter完成屏幕适配-重建Android,增加GetX路由,屏幕适配,基础导航栏-多版本SDK以及gradle造成的关于fvm的使用(flutter version manage)-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 2
    【01】噩梦终结flutter配安卓android鸿蒙harmonyOS 以及next调试环境配鸿蒙和ios真机调试环境-flutter项目安卓环境配置-gradle-agp-ndkVersion模拟器运行真机测试环境-本地环境搭建-如何快速搭建android本地运行环境-优雅草卓伊凡-很多人在这步就被难倒了
  • 3
    APP-国内主流安卓商店-应用市场-鸿蒙商店上架之必备前提·全国公安安全信息评估报告如何申请-需要安全评估报告的资料是哪些-优雅草卓伊凡全程操作
  • 4
    【03】仿站技术之python技术,看完学会再也不用去购买收费工具了-修改整体页面做好安卓下载发给客户-并且开始提交网站公安备案-作为APP下载落地页文娱产品一定要备案-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 5
    【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
  • 6
    【02】仿站技术之python技术,看完学会再也不用去购买收费工具了-本次找了小影-感觉页面很好看-本次是爬取vue需要用到Puppeteer库用node.js扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-优雅草卓伊凡
  • 7
    Cellebrite UFED 4PC 7.71 (Windows) - Android 和 iOS 移动设备取证软件
  • 8
    escrcpy:【技术党必看】Android开发,Escrcpy 让你无线投屏新体验!图形界面掌控 Android,30-120fps 超流畅!🔥
  • 9
    【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
  • 10
    即时通讯安全篇(一):正确地理解和使用Android端加密算法
  • 相关实验场景

    更多