The Code in the following pig activity:
Package com. ZTE. broadcastreceivermediascanner;
Import java. Io. file;
Import Android. App. activity;
Import Android. content. intent;
Import Android. content. intentfilter;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. OS. environment;
Import Android. View. view;
Import Android. widget. Button;
Public class broadcastreceivermediascanneractivity extends Activity
{
/** Called when the activity is first created .*/
Private scansdreceiver;
Private string _ path [];
@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button mbutton = (button) findviewbyid (R. Id. button1 );
Mbutton. setonclicklistener (New button. onclicklistener ()
{
Public void onclick (view arg0)
{
Scansdca ();
}
});
}
Private void scansdca ()
{
Intentfilter = new intentfilter (
Intent. action_media_scanner_started );
Intentfilter. addaction (intent. action_media_scanner_finished );
Intentfilter. adddatascheme ("file ");
Scansdreceiver = new scansdreceiver ();
Registerreceiver (scansdreceiver, intentfilter );
Sendbroadcast (new intent (intent. action_media_mounted,
Uri. parse ("file ://"
+ Environment. getexternalstoragedirectory ()
. Getabsolutepath ())));
}
}
Send broadcast information by clicking the button, and then scan the audio information through the broadcast service.
The following is the broadcast service code:
Package com. ZTE. broadcastreceivermediascanner;
Import Android. App. alertdialog;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. database. cursor;
Import Android. provider. mediastore;
Import Android. util. log;
Import Android. widget. Toast;
Public class scansdreceiver extends broadcastreceiver
{
Private alertdialog. Builder = NULL;
Private alertdialog ad = NULL;
Private int count1;
Private int count2;
Private int count;
@ Override
Public void onreceive (context, intent)
{
String action = intent. getaction ();
If (intent. action_media_scanner_started.equals (Action ))
{
Cursor C1 = context. getcontentresolver (). Query (
Mediastore. Audio. Media. external_content_uri,
New String [] {mediastore. Audio. Media. title,
Mediastore. Audio. Media. duration,
Mediastore. Audio. Media. artist,
Mediastore. Audio. Media. _ id,
Mediastore. Audio. Media. display_name}, null, null,
Null );
Count1 = c1.getcount ();
// System. Out. println ("count:" + count );
Log. I ("tag", "Count ---- >>>" + count );
Builder = new alertdialog. Builder (context );
Builder. setmessage ("scanning the memory card ...");
AD = builder. Create ();
Ad. Show ();
} Else if (intent. action_media_scanner_finished.equals (Action ))
{
Cursor C2 = context. getcontentresolver (). Query (
Mediastore. Audio. Media. external_content_uri,
New String [] {mediastore. Audio. Media. title,
Mediastore. Audio. Media. duration,
Mediastore. Audio. Media. artist,
Mediastore. Audio. Media. _ id,
Mediastore. Audio. Media. display_name}, null, null,
Null );
Count2 = c2.getcount ();
Log. I ("RG", "Count ---- >>>>>> ");
Count = count2-count1;
Log. I ("tag", "count2-count ---- >>>" + count );
Ad. Cancel ();
If (count> = 0)
{
Toast. maketext (context, "added" + Count + "First Song ",
Toast. length_long). Show ();
} Else
{
Toast. maketext (context, "total reduction" + Count + "First Song ",
Toast. length_long). Show ();
}
}
}
}