android监测用户刚刚拍照

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>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值