日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2024/9/27 65 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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: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>

  • 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);

    ??? }

    }

  • 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為例:

    點擊殺死進程前:

    點擊殺死進程之后的效果:

    ?

    ?

    ?

    ?

    總結

    以上是生活随笔為你收集整理的20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。