日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

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

發(fā)布時(shí)間:2024/9/27 Android 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  • 場(chǎng)景:實(shí)現(xiàn)安裝一個(gè)apk應(yīng)用程序的過(guò)程。界面如下:

  • 編寫如下應(yīng)用,應(yīng)用結(jié)構(gòu)如下:

  • <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="請(qǐng)輸入安裝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的內(nèi)容如下:

  • 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清單文件內(nèi)容如下:

  • <?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 場(chǎng)景:通過(guò)WebView加載一個(gè)頁(yè)面,程序運(yùn)行后的界面如下(注意:下面的頁(yè)面是通過(guò)WebView通過(guò)load的方式加載進(jìn)去的):

    程序案例結(jié)構(gòu)如下:

    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通知,界面效果:

    點(diǎn)擊點(diǎn)擊顯示通知,發(fā)現(xiàn)上方出現(xiàn)了一個(gè)紅色小人的通知

    點(diǎn)擊新版點(diǎn)擊顯示通知,將通知滑動(dòng)顯示查看,效果如下:

    程序案例結(jié)構(gòu)如下:

    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="點(diǎn)擊顯示通知" />

    ??? <Button

    ??????? android:onClick="click2"

    ??????? android:layout_width="wrap_content"

    ??????? android:layout_height="wrap_content"

    ??????? android:layout_centerHorizontal="true"

    ??????? android:text="新版點(diǎn)擊顯示通知" />

    ???

    </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,

    ?????????? "我是一個(gè)通知", 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);

    ?????? //設(shè)置點(diǎn)擊事件的類型

    ?????? notification.setLatestEventInfo(this, "我是標(biāo)題", "我是內(nèi)容", contentIntent);

    ?????? nm.notify(0, notification);

    ??? }

    ???

    ??? /**

    ??? ?* 新版本的notification

    ??? ?* @param view

    ??? ?*/

    ??? @SuppressLint("NewApi")

    ??? public void click2(View view){

    ?????? ?Notification noti = new Notification.Builder(this)

    ???????? .setContentTitle("我是標(biāo)題")? //這些都是Android版本11版本開(kāi)始的

    ???????? .setContentText("我是內(nèi)容")

    ???????? .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????????自殺代碼:殺死進(jìn)程的代碼如下:

    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("確定退出當(dāng)前應(yīng)用程序嗎?");

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

    ??????????

    ?????????? public void onClick(DialogInterface dialog, int which) {

    ????????????? finish();//關(guān)閉當(dāng)前的activity

    ????????????? //把自己的進(jìn)程殺死

    ????????????? //自殺的方法

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

    ?????????? }

    ?????? });

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

    ?????? builder.show();

    ??? }

    }

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

    退出后:

    ?

    ?

    1 殺死進(jìn)程,場(chǎng)景:殺死空進(jìn)程,后臺(tái)進(jìn)程,他殺(也就是說(shuō)殺不死自己),界面效果:

    程序案例代碼結(jié)構(gòu)如下:

    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="請(qǐng)輸入要?dú)⑺赖倪M(jìn)程包名"

    ??????? 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="殺死進(jìn)程"/>

    </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;//相當(dāng)于進(jìn)程管理器

    ??? private EditText et_packname;

    ?

    ??? protected void onCreate(Bundle savedInstanceState) {

    ?????? super.onCreate(savedInstanceState);

    ?????? setContentView(R.layout.activity_main);

    ??????

    ?????? //所有程序的開(kāi)啟都是由ActivityActivityManager來(lái)管理的

    ?????? 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();

    ?????? //注意要?dú)⑺绖e的進(jìn)程需要加上權(quán)限

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

    ?????? //這里中方式只是殺死別人,注意:手機(jī)里面有些進(jìn)程是殺死不掉的,比如電話進(jìn)程

    ?????? am.killBackgroundProcesses(packname);

    ??? }

    }

    程序運(yùn)行結(jié)果:

    注意:如果有時(shí)候提示要重啟adb,可以通過(guò)下面的方式:

    下面以殺死上面的com.android.music為例:

    點(diǎn)擊殺死進(jìn)程前:

    點(diǎn)擊殺死進(jìn)程之后的效果:

    ?

    ?

    ?

    ?

    總結(jié)

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

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。