android 开机启动

1) First, you need the permission in your AndroidManifest.xml:

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

 

2) Also, in yourAndroidManifest.xml, define your service and listen for the BOOT_COMPLETED action:

 

<service android:name=".MyService" android:label="My Service">
    <intent-filter>
        <action android:name="com.myapp.MyService" />
    </intent-filter>
</service>

<receiver
    android:name=".receiver.StartMyServiceAtBootReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="StartMyServiceAtBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

 

3) Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.

 

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent serviceIntent = new Intent(context, MySystemService.class);
            context.startService(serviceIntent);
        }
    }
}

And now your service should be running when the phone starts up.

 

4) 如果是开机启动Activity,特别注意加Intent.FLAG_ACTIVITY_NEW_TASK

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent activityIntent = new Intent(context, MyActivity.class);
	    activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(activityIntent);
        }
    }
}

参考: http://stackoverflow.com/questions/6391902/how-to-start-an-application-on-startup

android apk 反编译

1) 先去下个apktool 

2)       linux 下的安装说明

参考: https://code.google.com/p/android-apktool/wiki/Install

3) 反编译,回编译的操作说明

apktool d  **.apk

apktool d -s   **.apk                 得到 classes.dex

参考教程:https://code.google.com/p/android-apktool/wiki/ApktoolOptions

4) dex2jar

d2j-dex2jar.shclasses.dex

参考教程:https://code.google.com/p/dex2jar/wiki/UserGuide

5) jd-gui

运行时出错

./jd-gui: error while loading shared libraries: libgtk-x11-2.0.so.0: cannot open shared object file: No such file or directory

参考: http://blog.sina.com.cn/s/blog_73eefef20101cwt2.html

 

反驳:android不需要关闭后台运行程序的谬论

1. 手机很容易没电,很火大,我猜有东西在后台跑,想网上搜搜有没有关闭后台程序的方法

2. 在百度贴吧上看到这篇文章【Android原理 其实Android不需要关闭后台运行程序

文章中提到: Android原理你知道吗? 其实Android不需要关闭后台运行程序!洋洋洒洒写了一堆,其实就是说,

贴吧楼主的观点是:android的应用在被切换到后台时,它其实已经被暂停了,并不会消耗cpu资源,只保留了运行状态,这样等下次再次启动的时候,程序能达到启动快的效果

3. 开始反驳,用代码,用log说话

public class MainActivity extends Activity {

	private final String TAG = "saiwei.Test_AutoStart";

	private final int MSG_PRINT_LOG = 1;

	private int count = 0;

	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_PRINT_LOG:
				count++;
				//打印信息
				Log.i(TAG, "count="+count);
				//每1秒发送一次消息
				sendEmptyMessageDelayed(MSG_PRINT_LOG, 1000);
				break;
			default:
				break;
			}
		}
	};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mHandler.sendEmptyMessage(MSG_PRINT_LOG);
    }

    @Override
    protected void onResume() {
    	super.onResume();
    	Log.i(TAG, "onResume()");
    }

    @Override
    protected void onPause() {
    	super.onPause();
    	Log.i(TAG, "onPause()");
    }
}

LOG如下:

I/saiwei.Test_AutoStart(13759): count=10051
I/saiwei.Test_AutoStart(13759): count=10052
I/saiwei.Test_AutoStart(13759): count=10053
I/saiwei.Test_AutoStart(13759): count=10054
I/saiwei.Test_AutoStart(13759): count=10055
I/saiwei.Test_AutoStart(13759): count=10056
I/saiwei.Test_AutoStart(13759): count=10057
I/saiwei.Test_AutoStart(13759): count=10058
I/saiwei.Test_AutoStart(13759): count=10059
I/saiwei.Test_AutoStart(13759): count=10060
I/saiwei.Test_AutoStart(13759): count=10061
I/saiwei.Test_AutoStart(13759): count=10062

4) 在android手机上安装这个小程序,运行此程序,然后点击HOME键切换到后台,观察logcat,它还在计数,
   我在接电话,它在计数;
   我在打电话,它在计数;
   我在听歌,玩游戏,它在计数;
   我把屏幕关了,它还在计数…

由于我的手机是2G内存,它跑了很久都没被系统,在时时刻刻在运行着,在慢慢的消耗着我的电量,那位贴吧楼主说的“android的应用在被切换到后台时,它其实已经被暂停了,并不会消耗cpu资源”,真是忽悠人
5) 假如我在这个小程序上增加开机自动启动功能和被杀就自启动功能,这个就变成流氓程序了,它会一直在后台跑,一直在耗电,还且你还很难查出来。


导出android手机上已安装的软件(即:apk)

1. 以前一直很好奇,手机没root

2. 通过adb shell 连上手机, 进入/data/app  或 /system/app 都会提示【Permission denied】

3. 可是手机上的QQ竟然可以点击发送文件的时候,将本地apk和系统apk 发送出来

原理:

a)  通过 getPackageManager().getInstalledPackages 可以得到PackageInfo的集合

b)  然后通过 PackageInfo.applicationInfo.publicSourceDir 可以获取到软件apk的位置

比如打出的log如下:

publicSourceDir=/system/app/Calculator.apk
publicSourceDir=/data/app/com.tencent.mm-2.apk
publicSourceDir=/system/app/Calculator.apk
publicSourceDir=/system/app/Bluetooth.apk

c)  剩下就是文件流的操作问题了,参考:http://106.186.118.123/wordpress/wp-admin/post.php?post=427&action=edit

java copy file

1) 4 Ways to Copy File in Java
----1. Copy File Using FileStreams
----2. Copy File using java.nio.channels.FileChannel
----3. Copy File using Apache Commons IO
----4. Copy File using Java 7 Files class

 


package com.javacodegeeks.java.core;</pre>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;

public class CopyFilesExample {

public static void main(String[] args) throws InterruptedException,
IOException {

File source = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile1.txt");
File dest = new File("C:\\Users\\nikos7\\Desktop\\files\\destfile1.txt");

// copy file using FileStreams
long start = System.nanoTime();
long end;
copyFileUsingFileStreams(source, dest);
System.out.println("Time taken by FileStreams Copy = "
+ (System.nanoTime() - start));

// copy files using java.nio.FileChannel
source = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile2.txt");
dest = new File("C:\\Users\\nikos7\\Desktop\\files\\destfile2.txt");
start = System.nanoTime();
copyFileUsingFileChannels(source, dest);
end = System.nanoTime();
System.out.println("Time taken by FileChannels Copy = " + (end - start));

// copy file using Java 7 Files class
source = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile3.txt");
dest = new File("C:\\Users\\nikos7\\Desktop\\files\\destfile3.txt");
start = System.nanoTime();
copyFileUsingJava7Files(source, dest);
end = System.nanoTime();
System.out.println("Time taken by Java7 Files Copy = " + (end - start));

// copy files using apache commons io
source = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile4.txt");
dest = new File("C:\\Users\\nikos7\\Desktop\\files\\destfile4.txt");
start = System.nanoTime();
copyFileUsingApacheCommonsIO(source, dest);
end = System.nanoTime();
System.out.println("Time taken by Apache Commons IO Copy = "
+ (end - start));

}

private static void copyFileUsingFileStreams(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}

private static void copyFileUsingFileChannels(File source, File dest)
throws IOException {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}

private static void copyFileUsingJava7Files(File source, File dest)
throws IOException {
Files.copy(source.toPath(), dest.toPath());
}

private static void copyFileUsingApacheCommonsIO(File source, File dest)
throws IOException {
FileUtils.copyFile(source, dest);
}

}

Time taken by FileStreams Copy = 127572360
Time taken by FileChannels Copy = 10449963
Time taken by Java7 Files Copy = 10808333
Time taken by Apache Commons IO Copy = 17971677

 

As you can see FileChannels is the best way to copy large files. If you work with even larger files you will notice a much bigger speed difference.

参考链接: http://examples.javacodegeeks.com/core-java/io/file/4-ways-to-copy-file-in-java/