现在的位置: 首页 > 综合 > 正文

android 文件查找具体实现详解

2018年06月09日 ⁄ 综合 ⁄ 共 4678字 ⁄ 字号 评论关闭

总结文件查询的几个过程:

1、主线程获取查询关键字和查询路径。

2、主线程启动查询服务,并在查询服务里用处理线程查询。

3、查询完成后,把数据广播出去

4、在主线程里的广播接收器接收广播的查询结果,并对结果进行显示等相关处理。

5、在主线程的广播接收器里关闭停止文件查询服务。

一、启动文件查询服务前的处理

1、点击查询,弹出查询对话框

在此处获取查找的关键字和要查询的路径。

2、向查询广播接收器发送广播

//构造一个被广播的意图intent
Intent keywordIntent = new Intent();
//定义意图动作
keywordIntent.setAction(KEYWORD_BROADCAST);
//intent放入路径参数												
keywordIntent.putExtra("searchpath", mSDCard);
//intent放入关键字参数
keywordIntent.putExtra("keyword", keyWords);
//发送广播												
getApplicationContext().sendBroadcast(keywordIntent);			

根据意图的动作和注册的广播接收器,可知是SearchBroadCast类接收该广播

3、SearchBroadCast广播接收器的实现:

public class SearchBroadCast extends BroadcastReceiver 
{	
	public static  String mServiceKeyword = "";//接收搜索关键字的静态变量
    public static  String mServiceSearchPath = "";//接收搜索路径的静态变量   
	@Override
	public void onReceive(Context context, Intent intent) 
	{
		//取得intent
		String mAction = intent.getAction();
		if(MainActivity.KEYWORD_BROADCAST.equals(mAction))
		{
			//取得intent传递过来的信息
			mServiceKeyword = intent.getStringExtra("keyword");
			mServiceSearchPath = intent.getStringExtra("searchpath");
		}
	}
}

在这里获取关键字和查询路径,该参数需要在查询服务中使用。

文件查询服务中要用到的参数:SearchBroadCast.mServiceKeyword、SearchBroadCast.mServiceSearchPath

4、启动查询服务器:

//通过隐式intent启动服务
serviceIntent = new Intent("com.android.service.FILE_SEARCH_START");												
//如果调用startService()方法前服务已经被创建,多次调用startService()方法并不会导致多次创建服务,
//但会导致多次调用onStart()方法,因此启动文件查询服务,可以同时查找多次
MainActivity.this.startService(serviceIntent);//开启服务,启动搜索

根据intent的action和注册文件AndroidManifest.xml里的如下代码:

<service android:name="FileService">
			<intent-filter>
				<action android:name="com.android.service.FILE_SEARCH_START" />
				<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</service>

可知道启动的服务为FileService

二、FileService文件查询服务的处理
1、在onCreate()函数中创建处理线程,在线程中查询。

//新建处理线程
		HandlerThread mHT = new HandlerThread("FileService",HandlerThread.NORM_PRIORITY);
		//启动线程
		mHT.start();
		//获取处理线程的Looper
		mLooper = mHT.getLooper();
		//用来向Looper的消息队列插入消息
		mFileHandler = new FileHandler(mLooper);

2、在onStart()函数中发送消息和搜索正在进行的通知
mFileHandler.sendEmptyMessage(0);

同时发出通知表明正在进行搜索
  fileSearchNotification();

3、发出的消息由FileHandler消息处理器处理

class FileHandler extends Handler
{			
		@Override
		public void handleMessage(Message msg) 
		{
		  //递归搜索函数:文件搜索的操作在此处进行
			initFileArray(new File(SearchBroadCast.mServiceSearchPath));
		}	
}	
	  
//具体做搜索事件的递归函数
private void initFileArray(File file)
{      
    File[] mFileArray = file.listFiles();
        	
    //从指定目录下的文件列表中取文件
    for(File currentArray:mFileArray)
    {
    		//判断文件名或文件夹名是否包含查询关键字
        if(currentArray.getName().indexOf(SearchBroadCast.mServiceKeyword) != -1)
        {       			        			
        			//把搜索的文件名称和路径添加进来
        			mFileName.add(currentArray.getName());
        			mFilePaths.add(currentArray.getPath());
         }
        		
        //如果是文件夹则递归调用该方法
        if(currentArray.exists()&¤tArray.isDirectory())
        {
        	//如果用户取消了搜索,应该停止搜索的过程
        	if(MainActivity.isComeBackFromNotification == true)
        	{
        		return;
        	}
         initFileArray(currentArray);
        }
     }
}            

4、文件搜索完成后,同样是在handleMessage函数里,把搜索的结果广播出去:

//根据注册的广播:FileBroadcast广播接收器,接收FILE_SEARCH_COMPLETED广播
Intent intent = new Intent(FILE_SEARCH_COMPLETED);
intent.putStringArrayListExtra("mFileNameList", mFileName);
ntent.putStringArrayListExtra("mFilePathsList", mFilePaths);
//搜索完毕之后携带数据并发送广播
sendBroadcast(intent); 

5、搜索结果广播接收器:FileBroadcast

class FileBroadcast extends BroadcastReceiver
{
    //该方法用于实现接收到广播的具体处理,其中参数intent:为接受到的intent
		@Override
		public void onReceive(Context context, Intent intent) 
		{
			//获取意图的动作
			mAction = intent.getAction();			
			// 搜索完毕的广播,接收广播传递过来的搜索结果
			if(FileService.FILE_SEARCH_COMPLETED.equals(mAction))
			{
				mFileName = intent.getStringArrayListExtra("mFileNameList");
				mFilePaths = intent.getStringArrayListExtra("mFilePathsList");
				Toast.makeText(MainActivity.this, "搜索完毕!", Toast.LENGTH_SHORT).show();				
				
				//******8显示搜索的结果**********//
				
				//当搜索完毕的时候停止服务,然后在服务中取消通知
				getApplicationContext().stopService(serviceIntent);
			}		
		}
}           

处理:获取结果,然后显示,然后关闭停止服务。

三、在查询的过程中可以停止查询,该功能通过通知Notification来实现

1、在服务的onStart()函数中发出通知:

//实例化通知:第一个参数代表图标,第二个参数代表提示的内容
//第三个参数是指要显示的时间,一般是当即显示,故填入系统当前时间
Notification mNotification = new Notification(R.drawable.logo,"后台搜索中...",System.currentTimeMillis());   	
//构建一个意图,action为FILE_NOTIFICATION
Intent intent = new Intent(FILE_NOTIFICATION);
//打开notice时的提示内容
intent.putExtra("notification", "当通知还存在,说明搜索未完成,可以在这里触发一个事件,当点击通知回到Activity之后,可以弹出一个框,提示是否取消搜索!");   	
//该语句的作用是定义了一个用来广播的intent,只有当用户拉下notify显示列表,并且单击对应的项的时候发送广播
PendingIntent mPI = PendingIntent.getBroadcast(this,0,intent,0);
//在此处设置在nority列表里的该norifycation得显示情况,点击后将发送PendingIntent对象
mNotification.setLatestEventInfo(this, "在"+SearchBroadCast.mServiceSearchPath+"下搜索", "搜索关键字为"+SearchBroadCast.mServiceKeyword+"【点击可取消搜索】", mPI);	
if(mNF == null)
{
   mNF = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}
mNF.notify(R.string.app_name, mNotification);

2、在手机的状态栏里,点击该通知时,根据上面的代码,会发送广播。点击后相当于执行下面操作:

Intent intent = new Intent(FILE_NOTIFICATION);

sendBroadcast(intent);

3、根据注册的广播,该广播被FileBroadcast接收

在FileBroadcast的onReceive这个函数里,判断action,如下:

if(FileService.FILE_NOTIFICATION.equals(mAction))
{
  //在此进行关闭的提示等相关处理
  //然后关闭文件搜索服务
  getApplicationContext().stopService(serviceIntent);
}

 

抱歉!评论已关闭.