zoukankan      html  css  js  c++  java
  • 默认开机启动;通过Broadcastreceiver广播监听开机启动,实现"没有activity"的自启服务或者自启应用程序。

    一、首先介绍一下如何开机启动:

    第一种:  监控RECEIVE_BOOT_COMPLETED,即开机启动事件

    第二种: 监控sd卡mount事件  开机总会扫描sd卡吧? 监控sd卡事件也有类似开机启动效果,特别app安装在sd卡的情况下有些os是抓取不到RECEIVE_BOOT_COMPLETED

    第三种: android:installLocation="internalOnly", 限制app的安装位置,使其能抓取到RECEIVE_BOOT_COMPLETED

    我们也可以同时使用上面的方法

    <receiver android:name="com.example.receiver.SystemEventReceiver"   
              android:permission="android.permission.RECEIVE_BOOT_COMPLETED">  
        <intent-filter>  
            <action android:name="android.intent.action.BOOT_COMPLETED" />   //监听开机启动
        </intent-filter>  
       <intent-filter>  
            <action android:name="RestartSerivcesForSystemEventReceiver" /> //监听系统服务的重启    
        </intent-filter>  
       <intent-filter>  
     <action android:name="android.intent.action.MEDIA_MOUNTED" />    //媒体安装,即sd卡的加载(仅个人翻译加猜想)
     <action android:name="android.intent.action.MEDIA_UNMOUNTED" />   //媒体卸载,即sd卡卸载
     <action android:name="android.intent.action.MEDIA_EJECT" />      //媒体弹出,即sd卡弹出
     <data android:scheme="file" >  
    </data>    
        </intent-filter>  
    </receiver>  
    

      

    二、由于广播不能单独的存在,必须要有相应的activity,我们可以在清单文件里修改activity的<category android:name="android.intent.category.DEFAULT" />,这样在安装的时候就不会存在图标了,然后静态注册监听开机事件,一开机就启动服务或者应用程序,这样我们就相当于完成了一个”没有activity的开机自启动其他应用程序或者启动某服务”

    同理也可以实现,没有图标监听某些事件而采取相应操作。

     服务里也能启动应用程序,activity和service都是继承context的,所有需要context的时候可以用this,broadcastreceiver是继承object的,但是在接受广播的时候会传进context和intent,所有也可以启动其他的服务或者应用程序。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.startselfreceiver"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
        <uses-permission  android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.startselfreceiver.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
            
            <service 
                android:name=".Myservice"
                >
                <intent-filter >
                    <action android:name="myservice"/>
                </intent-filter>
            </service>
            
            <receiver 
                android:name=".Myreceiver"
                >
                <intent-filter >
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                </intent-filter>
            </receiver>
            
        </application>
    
    </manifest>
    

      广播文件:

    package com.example.startselfreceiver;
    
    import android.content.BroadcastReceiver;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.widget.Toast;
    
    public class Myreceiver extends BroadcastReceiver {
    
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		System.out.println("收到广播了");
    		
    		Intent startserviceintent=new Intent("myservice");  //启动服务
    		context.startService(startserviceintent);
    		
    		PackageManager p=context.getPackageManager();
    		Intent in=p.getLaunchIntentForPackage("com.example.secondapp"); // 启动其他应用程序
    		if(in!=null)
    		{
    			
    			context.startActivity(in);
    		}
    		else
    		{
    			Toast.makeText(context, "哟,赶紧下载安装这个APP吧", Toast.LENGTH_LONG).show();
    		}
    				
    				
    //		Intent intent2 = new Intent("android.intent.action.MAIN");  
    //		intent2.addCategory("android.intent.category.LAUNCHER");              
    //		ComponentName cn = new ComponentName("com.example.secondapp", "com.example.secondapp.MainActivity");              
    //		intent2.setComponent(cn);  
    //		context.startActivity(intent2);  
     		
    		
    		
    	}
    
    }
    

      服务文件:

    package com.example.startselfreceiver;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileWriter;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import android.app.Service;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.os.Environment;
    import android.os.IBinder;
    
    public class Myservice extends Service {
    
    	@Override
    	public IBinder onBind(Intent intent) {
    		// TODO Auto-generated method stub
    		System.out.println("服务启动!");
    		return null;
    	}
    
    	@Override
    	public void onCreate() {
    		super.onCreate();
    		
    		 try {
    //	        	System.out.println(Environment.getDataDirectory().getAbsolutePath());
    				OutputStream out=openFileOutput("zp.txt", MODE_PRIVATE);//Environment.getDataDirectory().getAbsolutePath()+,输出流只能在工程目录下创建文件,前面的参数                                                   //不能包含路径
    				String st="my name is zp";
    				out.write(st.getBytes());
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		
    	}
    
    }
    

      

  • 相关阅读:
    跨域解决方法
    css之line-height
    untiy项目中使用MD5加密
    unity给子物体添加Shader
    unity中UI坐标转3d世界坐标
    unity项目字符串转为Vector3和Quaternion
    unity中使用Highlighting System v4.0插件给物体添加高亮
    加载AssetBundle方法
    Lua面向对象----类、继承、多继承、单例的实现
    Lua学习笔记(一)-----C#和lua的交互
  • 原文地址:https://www.cnblogs.com/bokeofzp/p/4734871.html
Copyright © 2011-2022 走看看