如何在Android下载应用程序时获取声音

时间:2012-04-26 06:46:47

标签: android notifications

我想在Android手机中下载应用程序时听到一些声音。

当我通过网络从Android市场下载应用程序时,我希望听到一些警笛声。任何人都可以建议我怎么能得到这个?

提前致谢

SoundActivity.java
------------------

    public class SoundActivity extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent)
        {
          if("android.intent.action.DOWNLOAD_COMPLETED".equals(intent.getAction()))
          {
              String PATH_TO_FILE = "/mnt/sdcard/MLKDream_64kb.mp3"; 
            //   MediaPlayer player=MediaPlayer.create(this, R.raw.sirensound);
                  MediaPlayer player = new MediaPlayer();
                  try {
                      Log.v("play mpe","play mpe");
                       player.setDataSource(PATH_TO_FILE);
                       player.prepare();
                       player.start();
                 } catch (IllegalArgumentException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                 } catch (IllegalStateException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                 } catch (IOException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                 }
          }

Manifest.xml
-------------
   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sound"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver 
        android:name=".SoundActivity"
        android:exported="true"
        android:icon="@drawable/ic_launcher" >
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
         </intent-filter>
     </receiver>

    </application>

1 个答案:

答案 0 :(得分:1)

下载完成后,系统可能会有一个广播形式,只需制作一个可以播放的应用,并在广播到达时播放声音。

像这样:

public class DownloadChecker extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent)
    {
      if("android.intent.action.DOWNLOAD_COMPLETED".equals(intent.getAction()))
      {
         //play a sound here 
      }
    }

}

Manifast.xml标记

中的application中尝试使用此功能
<receiver 
        android:name=".DownloadChecker "
        android:exported="true"
        android:icon="@drawable/icon" >
        <intent-filter>
            <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
         </intent-filter>
     </receiver>

我希望有效,我无法测试它