0

我想在安装我的应用程序时发出声音。我通过在我的应用程序中使用广播接收器来尝试这个。在广播接收器中,我正在运行服务以启动媒体播放器。但是我无法进入广播接收器的接收方法。但如果我尝试安装另一个应用程序,我会收到该事件。如何仅在我的应用程序中获取事件。我在清单文件中的权限

<uses-permission android:name = "android.permission.INSTALL_PACKAGES"/>
    <uses-permission android:name="android.permission.RESTART_PACKAGES"/>

<receiver android:name=".DemoReceiver" >
        <intent-filter >
            <action android:name="android.intent.action.PACKAGE_ADDED" />
            <action android:name="android.intent.action.PACKAGE_INSTALL" />
             <action android:name="android.intent.action.PACKAGE_CHANGED" />
              <action android:name="android.intent.action.PACKAGE_RESTARTED" />               
    <action android:name="android.intent.action.PACKAGE_REPLACED"/>

    <action android:name="android.intent.action.USER_PRESENT"/>
    <data android:scheme="package"/>
        </intent-filter>
    </receiver>

并在广播接收器中

import android.content.BroadcastReceiver;

导入android.content.Context;导入android.content.Intent;

public class DemoReceiver extends BroadcastReceiver {
     @Override
     public void onReceive(final Context context, final Intent bootintent) {
         System.out.println("entered broadcast receiver");
           if(bootintent.getAction() != null)
         {               
              context.startService(new Intent(context, DemoService.class));
         }

     }
     }

服务是

public class DemoService extends Service {

 MediaPlayer player;


 private class LogTask extends TimerTask {
  public void run() {
   Log.i(LOGTAG, "scheduled");
  }
 }
 private LogTask mLogTask;

 @Override
 public IBinder onBind(final Intent intent) {
  return null;
 }

 @Override
 public void onCreate() {
  super.onCreate();
  Log.v("StartServiceAtBoot", "StartAtBootService Created");
  player=MediaPlayer.create(this, R.raw.sirensound);
  player.setLooping(false);
 }

 public void onStart(Intent intent, int flags, int startId) {

       Log.v("StartServiceAtBoot", "StartAtBootService -- onStartCommand()");        


       player.start();


      }
 @Override
      public void onDestroy() {
            super.onDestroy();

            Log.v("StartServiceAtBoot", "StartAtBootService Destroyed");

      }

}
4

3 回答 3

1

如果您想从您的应用程序执行任何代码,它必须已经安装,所以我认为..当您的应用程序安装时,您无法从您的应用程序接收广播。

于 2012-04-30T05:59:45.103 回答
1

在我的应用程序中,当用户在我的应用程序运行时摇动设备时,我设法创建了声音......

public class SensorTest extends Activity implements SensorEventListener,
    OnCompletionListener {
private SensorManager sensorManager;
private boolean color = false;
private long lastUpdate;
private MediaPlayer mMediaplayer;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    sensorManager.registerListener(this, sensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);
    lastUpdate = System.currentTimeMillis();
}

public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        float[] values = event.values;

        float x = values[0];
        float y = values[1];
        float z = values[2];

        float accelationSquareRoot = (x * x + y * y + z * z)
                / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long actualTime = System.currentTimeMillis();
        if (accelationSquareRoot >= 2) //
        {
            if (actualTime - lastUpdate < 200) {
                return;
            }
            lastUpdate = actualTime;

            if (color) {

            } else {

                try {

                    AssetFileDescriptor afd = getAssets().openFd(
                            "gavel_single.wav");
                    mMediaplayer = new MediaPlayer();
                    mMediaplayer.setDataSource(afd.getFileDescriptor(), afd
                            .getStartOffset(), afd.getLength());
                    afd.close();
                    mMediaplayer.prepare();
                    mMediaplayer.start();

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }

    }

}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub

}

@Override
protected void onResume() {
    super.onResume();
    // register this class as a listener for the orientation and
    // accelerometer sensors
    sensorManager.registerListener(this, sensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
    // unregister listener
    sensorManager.unregisterListener(this);
    super.onStop();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mMediaplayer != null) {
        mMediaplayer.release();
        mMediaplayer = null;
    }

}

public void onCompletion(MediaPlayer mp) {
    mp.seekTo(0);
    mp.release();

}

}

于 2012-04-30T06:03:05.247 回答
0

您不能在标准的 Android 平台上执行此操作,至少在从正常来源分发时(您可能可以通过使用 javascript 打开网页和指向您自己服务器上的 apk 的链接来做某事,但用户必须首先在他们的设置中启用“未知来源”)

但是,您可以在首次运行时播放声音。但是,只有当用户从安装程序中单击“打开”或从主屏幕等启动它时,安装才会运行。

于 2012-04-30T06:05:43.483 回答