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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

默认开机启动;通过Broadcastreceiver广播监听开机启动,实现没有activity的自启服务或者自启应用程序。...

發(fā)布時(shí)間:2024/9/5 编程问答 87 豆豆
生活随笔 收集整理的這篇文章主要介紹了 默认开机启动;通过Broadcastreceiver广播监听开机启动,实现没有activity的自启服务或者自启应用程序。... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、首先介紹一下如何開機(jī)啟動(dòng):

第一種: ?監(jiān)控RECEIVE_BOOT_COMPLETED,即開機(jī)啟動(dòng)事件

第二種: 監(jiān)控sd卡mount事件 ?開機(jī)總會(huì)掃描sd卡吧? 監(jiān)控sd卡事件也有類似開機(jī)啟動(dòng)效果,特別app安裝在sd卡的情況下有些os是抓取不到RECEIVE_BOOT_COMPLETED

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

我們也可以同時(shí)使用上面的方法

<receiver android:name="com.example.receiver.SystemEventReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> //監(jiān)聽開機(jī)啟動(dòng)</intent-filter> <intent-filter> <action android:name="RestartSerivcesForSystemEventReceiver" /> //監(jiān)聽系統(tǒng)服務(wù)的重啟 </intent-filter> <intent-filter> <action android:name="android.intent.action.MEDIA_MOUNTED" />   //媒體安裝,即sd卡的加載(僅個(gè)人翻譯加猜想)<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>

  

二、由于廣播不能單獨(dú)的存在,必須要有相應(yīng)的activity,我們可以在清單文件里修改activity的<category android:name="android.intent.category.DEFAULT" />,這樣在安裝的時(shí)候就不會(huì)存在圖標(biāo)了,然后靜態(tài)注冊(cè)監(jiān)聽開機(jī)事件,一開機(jī)就啟動(dòng)服務(wù)或者應(yīng)用程序,這樣我們就相當(dāng)于完成了一個(gè)”沒有activity的開機(jī)自啟動(dòng)其他應(yīng)用程序或者啟動(dòng)某服務(wù)”

同理也可以實(shí)現(xiàn),沒有圖標(biāo)監(jiān)聽某些事件而采取相應(yīng)操作。

?服務(wù)里也能啟動(dòng)應(yīng)用程序,activity和service都是繼承context的,所有需要context的時(shí)候可以用this,broadcastreceiver是繼承object的,但是在接受廣播的時(shí)候會(huì)傳進(jìn)context和intent,所有也可以啟動(dòng)其他的服務(wù)或者應(yīng)用程序。

<?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-sdkandroid: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"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid: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 {@Overridepublic void onReceive(Context context, Intent intent) {System.out.println("收到廣播了");Intent startserviceintent=new Intent("myservice"); //啟動(dòng)服務(wù)context.startService(startserviceintent);PackageManager p=context.getPackageManager();Intent in=p.getLaunchIntentForPackage("com.example.secondapp"); // 啟動(dòng)其他應(yīng)用程序if(in!=null){context.startActivity(in);}else{Toast.makeText(context, "喲,趕緊下載安裝這個(gè)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); }}

  服務(wù)文件:

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 {@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("服務(wù)啟動(dòng)!");return null;}@Overridepublic void onCreate() {super.onCreate();try { // System.out.println(Environment.getDataDirectory().getAbsolutePath());OutputStream out=openFileOutput("zp.txt", MODE_PRIVATE);//Environment.getDataDirectory().getAbsolutePath()+,輸出流只能在工程目錄下創(chuàng)建文件,前面的參數(shù)                                                   //不能包含路徑String st="my name is zp";out.write(st.getBytes());} catch (Exception e) {e.printStackTrace();}}}

  

?

轉(zhuǎn)載于:https://www.cnblogs.com/bokeofzp/p/4734871.html

總結(jié)

以上是生活随笔為你收集整理的默认开机启动;通过Broadcastreceiver广播监听开机启动,实现没有activity的自启服务或者自启应用程序。...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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