希望有声音在安装应用程序时,(want to have sound when the app is

2019-09-16 20:54发布

我想安装我的应用程序时,有一个声音。 我通过我的应用程序使用broadcastreciever尝试这个。 在广播reciever IAM运行的服务来启动媒体播放器。 但IAM不能够进入的广播reciever的方法收到。 但是,如果我尝试安装另一个应用程序IAM获得该事件。 如何让只有我的应用程序的事件。 我在清单文件权限

<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>

并在广播reciever

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");

      }

}

Answer 1:

如果你想从你的应用程序执行任何代码,它必须已经安装了,所以安装应用程式时,我think..you不能从您的应用程序接收广播。



Answer 2:

在我的应用程序管理,以营造良好的当用户我的应用程序运行时震动装置....

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

}

}



Answer 3:

你不能做到这一点在标准Android平台,至少从一个正常的源分配时,(你也许可以用javascript和你自己的服务器上的APK的链接打开一个网页,做一些事情,但用户必须使在其设置中“不明来源”第一)

你可以,但是,播放声音第一次运行。 但是,一个只安装成为一个运行,如果用户点击从安装程序“打开”或从主屏幕上,等它启动



文章来源: want to have sound when the app is installed