Android自启动应用

来源:互联网 发布:日文图片翻译软件 编辑:程序博客网 时间:2024/05/22 00:34

Java文件:

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.content.pm.ApplicationInfo;

import android.content.pm.PackageManager;

import android.content.pm.ResolveInfo;

import android.os.Bundle;

import android.text.method.ScrollingMovementMethod;

import android.widget.TextView;

import java.util.Iterator;

import java.util.List;

publicclass MainActivityextends Activity {


    PackageManager packageManager =null;

    StringBuilder stringBuilder =null;

    TextView infoTextView=null;


    @Override

   protectedvoid onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        packageManager = getPackageManager();//获取PackageManager

        infoTextView = (TextView)findViewById(R.id.infoTextView);

        infoTextView.setMovementMethod(ScrollingMovementMethod.getInstance());

        infoTextView.setScrollbarFadingEnabled(false);


    }


    @Override

   protectedvoid onResume() {

       super.onResume();

        stringBuilder =new StringBuilder();

        List<ResolveInfo> resolveInfos = packageManager.queryBroadcastReceivers(new Intent(Intent.ACTION_BOOT_COMPLETED), PackageManager.GET_DISABLED_COMPONENTS);

       int appCount = resolveInfos.size();

        stringBuilder.append("本机共有"+appCount+"个应用\n");


       int autoStartAppCount =0;

       for (Iterator<ResolveInfo> iterator = resolveInfos.iterator();iterator.hasNext();){

            ResolveInfo resolveInfo = iterator.next();

            ApplicationInfo applicationInfo = resolveInfo.activityInfo.applicationInfo;

           if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) !=0){

               continue;

            }


            autoStartAppCount++;//统计可以自启动的程序个数

            String label = (String)packageManager.getApplicationLabel(applicationInfo);

            String packageName = applicationInfo.packageName;

            String statement=null;

            ComponentName componentName =new ComponentName(packageName,resolveInfo.activityInfo.name);

           if (packageManager.getComponentEnabledSetting(componentName) != PackageManager.COMPONENT_ENABLED_STATE_DISABLED){

                statement ="启用自启动";

            }else{

                statement ="禁用自启动";

            }

            stringBuilder.append(label+": "+statement+"\n");

            stringBuilder.append(packageName +"\n");

            stringBuilder.append("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");


        }

        stringBuilder.append("共有"+autoStartAppCount+"个应用自启动\n");


        infoTextView.setText(stringBuilder);

    }

}


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
 * Created by LiuMingchuan on 14-5-23.
 */
public class AutoStartBroadCastReciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            //启动本应用处理
            Intent autoStartIntent=new Intent(context,MainActivity.class);
            autoStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(autoStartIntent);
        }
    }
}

Layout文件:

<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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.liumingchuan.mygame.fetchautostart.MainActivity"
    android:background="#334422">


    <TextView
        android:id="@+id/infoTextView"
        android:textColor="#ff0000"
        android:text="@string/hello_world"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView"/>


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/icon"/>


</RelativeLayout>

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.liumingchuan.mygame.fetchautostart" >
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.liumingchuan.mygame.fetchautostart.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>
        <receiver android:name=".AutoStartBroadCastReciver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.HOME"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

0 0
原创粉丝点击