It is now 2014-07-16 15:27.
Long time no writing, suddenly inspired to write something (in fact, just got a tricky question, proud of it. Oh, I'm a cow. Don't say much nonsense, get to the point.
Do not know you do not encounter this problem, itself do an application, you can listen to the boot broadcast. But it's very strange that the version number under the android3.0 is no problem for you to run. But the version number above android3.0 is afraid the situation is different. You will find that there are often very many times when you do not receive a boot broadcast. What is this for? Hey, don't tell you! In fact, this area Baidu very many people give why. I don't have much nonsense here, and today we're going to talk about solutions.
Well, given the solution, there are two types of online:
1. Join Permissions <uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>
2. Upgrade your app to a system app
Let's start with the first kind of thing. I tried to look practical under Android 4.0. But if you use Android version number 4.0 or more of the machine you will find the same as the old, the broadcast can not receive.
Well, it seems that there is only one other way, ah hehe just what I want to say today is another way. Start!!!!
Prerequisite Preparation:
1. A machine that has been rooted successfully. What the? Don't know how to root? Don't ask me what tools are on the market.
2. Oh, no, 2. You just have to prepare a machine that is already rooted successfully. To say something hard, Java environment Eclipse Android environment.
Basic idea: I originally installed the app is to receive the power of the broadcast, but the system is not sent to our own writing applications, but careful you will find that every time you turn on or off our Android phone will send a boot broadcast ( Android.intent.action.BOOT_COMPLETED), only the app we do can't receive it. However, the mobile phone's system-level app is bound to be accepted. OK, so suppose we have a way to make the app that we do to be a system-level app, does that mean that you can receive a boot broadcast as well? OK, let's try it out. How?
First, if I write a test02.apk now.
Let's take a look at test02.apk what's going on in this project. Take a look at its androidmanifest.xml file
<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "com.example.test02"
Android:versioncode= "1"
Android:versionname= "1.0" >
<uses-sdk
Android:minsdkversion= "8"
android:targetsdkversion= "19"
Android:shareduserid= "Android.uid.system"/>
<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<activity
Android:name= "Com.example.test02.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= "Com.example.test02.BrocatTest" >
<intent-filter>
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
<category android:name= "Android.intent.category.HOME"/>
</intent-filter>
</receiver>
</application>
</manifest>
A receiver was declared to receive a power-on broadcast, so let's see what this brocattest did.
public class Brocattest extends broadcastreceiver{
@Override
public void OnReceive (Context arg0, Intent arg1) {
TODO auto-generated Method Stub
LOG.I ("Brocattest", "Run here .....");
}
}
Hey.. It's just a word of print .....
And then we get test02.apk, and we put it in the computer D-disk folder.
Open cmd into adb shell input command adb push d:\test02.apk/sdcard/copy test02.apk from the D-drive folder to the SDcard card folder of the phone
ADB shell
Su
Mount
See if your phone system directory is in a writable state (RW), by default it is read Only (RO)
If you are in a read-only state (RO), change the state to a writable state.
Mount-o remount,rw-t-yaffs2/dev/block/mtdblock3/system
Mount uses this command again to see if the state has changed
Assuming the state has changed the run
cat/sdcard/test02.apk >/system/app/test02.apk
After running the test02.apk has been successfully written to the/system/app directory, this directory is dedicated to store the system app place, rest assured. The phone will take the initiative to help you install it, assuming you want to confirm.
Cd/system/app
Ls
See if there's test02.apk this APK package
Finally, don't forget to run Mount-o-remount,ro-t-yaffs2/dev/block/mtdblock3/system change the status value back
Exit
Exit
Then restart the phone to see. You'll get a surprise outside.
All right, all of this is going to be done through the ADB shell, so does it say that I write in the code and I run it directly?
Ha-ha has come immediately.
We create a new project in Eclipse Test06 folder such as the following
First we put the APK package we want to promote into the system app into the assets directory
Then let's take a look at the Sdcardutil class, the job of this class is to write the test02.apk file under the assets folder in a stream to the phone's sdcard
/**
* For SD card operation class
* @author yangmo*/
public class Sdcardutil {
Private context context;
Private Boolean hascard = false; Infer if there is an SD card
Private String Sdpath = null;
Private String FilePath = null;
/**
* Constructor Function */
Public Sdcardutil (Context context) {
This.context = context;
Hascard = Environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED);
try {
Sdpath = Environment.getexternalstoragedirectory (). Getcanonicalpath ();
FilePath = This.context.getFilesDir (). GetPath ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System.out.println ("Sdpath =" +sdpath);
System.out.println ("FilePath =" +filepath);
}
public Boolean isOk () {
return hascard;
}
/**
* Write file to sd card */
public int Readstreamtosdcard (InputStream is, String fileName) {
int state =-1;
try{
FileOutputStream fos = new FileOutputStream (Sdpath + "/" +filename);
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read (buffer))! =-1) {
Fos.write (buffer, 0, count);
}
Fos.close ();
Is.close ();
state = 0;
return state;
}catch (Exception e) {
E.printstacktrace ();
return-1;
}
}
}
Let's take a look at the Rootcmd class, and this kind of basic job is to replace our adb shell
Public final class Rootcmd {
Run the Linux command and output the results
protected static string Execrootcmd (String paramstring) {
String result = "Result:";
try {
Process localprocess = Runtime.getruntime (). EXEC ("su");
The root-handled Android system has the SU command
OutputStream Localoutputstream = Localprocess.getoutputstream ();
DataOutputStream Localdataoutputstream = new DataOutputStream (localoutputstream);
InputStream Localinputstream = Localprocess.getinputstream ();
DataInputStream Localdatainputstream = new DataInputStream (localinputstream);
String str1 = string.valueof (paramstring);
String str2 = str1 + "\ n";
Localdataoutputstream.writebytes (STR2);
Localdataoutputstream.flush ();
String STR3 = null;
while ((STR3 = Localdatainputstream.readline ()) = null) {
LOG.D ("result", STR3);
// }
Localdataoutputstream.writebytes ("exit\n");
Localdataoutputstream.flush ();
Localprocess.waitfor ();
return result;
} catch (Exception localexception) {
Localexception.printstacktrace ();
return result;
}
}
Run the Linux command without paying attention to the result output
protected static int execrootcmdsilent (String paramstring) {
try {
Process localprocess = Runtime.getruntime (). EXEC ("su");
Object localobject = Localprocess.getoutputstream ();
DataOutputStream Localdataoutputstream = new DataOutputStream ((OutputStream) localobject);
String str = string.valueof (paramstring);
Localobject = str + "\ n";
Localdataoutputstream.writebytes ((String) localobject);
Localdataoutputstream.flush ();
Localdataoutputstream.writebytes ("exit\n");
Localdataoutputstream.flush ();
Localprocess.waitfor ();
int result = Localprocess.exitvalue ();
return (Integer) result;
} catch (Exception localexception) {
Localexception.printstacktrace ();
return-1;
}
}
Infer if the machine Android is root, that is, whether to get root privileges
Protected static Boolean Haveroot () {
int i = execrootcmdsilent ("echo Test");
Test by running a test command.
if (i! =-1) {
return true;
}
return false;
}
}
And then we'll take a look at what's going on in our main activity.
public class Mainactivity extends actionbaractivity{
private static String FILENAME = "test02.apk";
private int state =-1;
ADB shell command
String paramstring= "adb shell" + "\ n" +
"su" + "\ n" +
"Mount-o remount,rw-t yaffs2/dev/block/mtdblock3/system" + "\ n" +
"CAT/SDCARD/TEST02.APK >/system/app/test02.apk" + "\ n" +
"Mount-o remount,ro-t yaffs2/dev/block/mtdblock3/system" + "\ n" +
"Exit" + "\ n" +
"Exit";
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Sdcardutil sdutil = new Sdcardutil (this);
try {
if (Sdutil.isok ()) {
InputStream is = Getapplicationcontext (). Getassets (). open (FILENAME);
State = Sdutil.readstreamtosdcard (is, FILENAME);
}else
Toast.maketext (this, Getapplicationcontext (). getString (R.string.sd_title), Toast.length_short). Show ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
if (state = = 0) {//Everything is ready to be promoted to system-level applied actions
if (Rootcmd.haveroot ()) {
if (Rootcmd.execrootcmdsilent (paramstring) ==-1) {
Toast.maketext (this, Getapplicationcontext (). getString (R.string.initialiseok), Toast.length_long). Show ();
}else{
Toast.maketext (this, Getapplicationcontext (). getString (R.string.initialisefail), Toast.length_long). Show ();
}
}else{
Toast.maketext (this, Getapplicationcontext (). getString (R.string.noroot), Toast.length_long). Show ();
}
}
}
}
Look here first.
try {
if (Sdutil.isok ()) {
InputStream is = Getapplicationcontext (). Getassets (). open (FILENAME);
State = Sdutil.readstreamtosdcard (is, FILENAME);
}else
Toast.maketext (this, Getapplicationcontext (). getString (R.string.sd_title), Toast.length_short). Show ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
This part of the code first write the APK into our SD card
if (state = = 0) {//Everything is ready to be promoted to system-level applied actions
if (Rootcmd.haveroot ()) {
if (Rootcmd.execrootcmdsilent (paramstring) ==-1) {
Toast.maketext (this, Getapplicationcontext (). getString (R.string.initialiseok), Toast.length_long). Show ();
}else{
Toast.maketext (this, Getapplicationcontext (). getString (R.string.initialisefail), Toast.length_long). Show ();
}
}else{
Toast.maketext (this, Getapplicationcontext (). getString (R.string.noroot), Toast.length_long). Show ();
}
}
And this is part of our manual adb shell operation.
Note that the declaration of this attribute is paramstring
ADB shell command
String paramstring= "adb shell" + "\ n" +
"su" + "\ n" +
"Mount-o remount,rw-t yaffs2/dev/block/mtdblock3/system" + "\ n" +
"CAT/SDCARD/TEST02.APK >/system/app/test02.apk" + "\ n" +
"Mount-o remount,ro-t yaffs2/dev/block/mtdblock3/system" + "\ n" +
"Exit" + "\ n" +
"Exit";
nn, OK, connect your phone run Test06 this project to see .... hehe ~
Resolve the android3.0 version number above the application does not receive the boot broadcast issue