我希望在安装应用程序时发出声音。我在我的应用程序中使用broadcastreciever尝试了这个。在广播接收器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>
并在广播接收器中
import android.content.BroadcastReceiver;
import android.content.Context; import 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");
}
}
答案 0 :(得分:1)
如果你想从你的应用程序执行任何代码,它必须已经安装,所以我认为..当你的应用程序安装完毕后,你无法从你的应用程序接收广播。
答案 1 :(得分: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();
}
}
答案 2 :(得分:0)
你不能在标准的Android平台上做到这一点,至少在从普通来源分发时(你可以通过在自己的服务器上打开带有javascript的网页和指向apk的链接来做某事,但是用户必须首先在他们的设置中启用“未知来源”
但是,您可以在首次播放时播放声音。但是,如果用户从安装程序单击“打开”或从主屏幕启动它,则安装仅变为运行