Android scans Audio Information in sdcard through broadcast

Source: Internet
Author: User

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 ();
}
}
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.