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

来源:互联网 发布:mysql 表空间查看 编辑:程序博客网 时间:2024/04/30 13:20

  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为例:

点击杀死进程前:

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

 

 

 

 

0 0