package com.renlei.imgaemest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendBroadcast(new Intent().setAction(AutoStartReceiver.AUTO_START));
}
}
Receiver
package com.renlei.imgaemest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by renlei
* DATE: 15-10-13
* Time: 下午3:16
*/
public class AutoStartReceiver extends BroadcastReceiver {
public static final String AUTO_START = "auto_start";
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context,PhotoService.class);
context.startService(intent1);
Log.d("renlei AutoStartReceiver" ,"startService");
}
}
Service
package com.renlei.imgaemest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import java.io.File;
/**
* Created by renlei
* DATE: 15-10-13
* Time: 下午3:15
*/
public class PhotoService extends Service {
public static long totalSpace = 0;
private SharedPreferences sharedPreferences;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
sharedPreferences = getSharedPreferences("renlei", Context.MODE_MULTI_PROCESS);
new Thread(new DirUtilThread()).start();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("renlei","start service");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("renlei","PhotoService ondestory");
sendBroadcast(new Intent(AutoStartReceiver.AUTO_START));
}
class DirUtilThread implements Runnable{
@Override
public void run() {
File sdDir = null;
boolean sdIsExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
sdDir = new File(Environment.getExternalStorageDirectory()+"/DCIM/Camera/");
sharedPreferences.edit().putLong("size",getFileCount(sdDir)).commit();
long size = 0;
if (sdIsExist){
while (true){
if (sdDir.exists()){
size = getFileCount(sdDir);
Log.d("renlei2","size"+size);
if (size>sharedPreferences.getLong("size",0)){
Log.d("renlei","size"+size);
Log.d("renlei","************");
sharedPreferences.edit().putLong("size",size).commit();
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.defaults = Notification.DEFAULT_ALL;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.icon = R.drawable.ic_launcher;
notification.setLatestEventInfo(PhotoService.this,"renlei"+size,"renlei",null);
notificationManager.notify(R.string.app_name,notification);
}else if(size<sharedPreferences.getLong("size",0)){
sharedPreferences.edit().putLong("size",size).commit();
}
}
}
}
}
}
private long getFileSize(File file){
if (file.exists()){
if (file.isFile()){
return file.length();
}else {
long total = 0;
File []childs = file.listFiles();
if (childs.length>0){
for (int i = 0;i<childs.length;i++){
if (!childs[i].toString().endsWith(".mp4")){
total += childs[i].length();
}else {
}
}
}
return total;
}
}else {
return 0;
}
}
private long getFileCount(File file){
if (file.exists()){
if (file.isFile()){
return 1;
}else {
long total = 0;
File []childs = file.listFiles();
if (childs.length>0){
for (int i = 0;i<childs.length;i++){
if (!childs[i].toString().endsWith(".mp4")){
total += 1;
}else {
}
}
}
return total;
}
}else {
return 0;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.renlei.imgaemest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="15"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service android:name=".PhotoService"/>
<receiver android:name=".AutoStartReceiver">
<intent-filter>
<action android:name="auto_start"></action>
</intent-filter>
</receiver>
</application>
</manifest>