我要的是我的安装应用程序时,有一个声音。我在我的应用程序中使用broadcastreciever尝试这个。在广播reciever亚姆运行的服务来启动媒体播放器。但IAM无法进入在广播reciever的方法收到。但是,如果我尝试安装另一个应用程序IAM获得该事件。如何让只在我的应用程序的事件。我的清单文件的权限
<使用许可权的android:NAME =android.permission.INSTALL_PACKAGES/> <使用许可权的android:NAME =android.permission.RESTART_PACKAGES/><接收机器人:DemoReceiverNAME => &所述;意图滤光器> <作用机器人:名字=android.intent.action.PACKAGE_ADDED/> <作用机器人:名字=android.intent.action.PACKAGE_INSTALL/> <作用机器人:名字=android.intent.action.PACKAGE_CHANGED/> <作用机器人:名字=android.intent.action.PACKAGE_RESTARTED/> <作用机器人:名字=android.intent.action.PACKAGE_REPLACED/> <作用机器人:名字=android.intent.action.USER_ preSENT/> <数据机器人:计划=包/> &所述; /意图滤光器> < /接收器>
和在广播reciever
进口android.content.BroadcastReceiver;
进口android.content.Context;进口android.content.Intent;
公共类DemoReceiver扩展广播接收器{ @覆盖 公共无效的onReceive(最终上下文的背景下,最终的意图bootintent){ 的System.out.println(进入广播接收机); 如果(bootintent.getAction()!= NULL) { context.startService(新意图(上下文,DemoService.class)); } } }
![会声会影安装过程中需要删除应用程序是什么意思,会不会对电脑造成不好的影响或者导致某些程序异常](https://p.xsw88.cn/allimgs/daicuo/20230907/5162.png)
和服务是
公共类DemoService延伸服务{ MediaPlayer的播放器; 私有类LogTask扩展的TimerTask { 公共无效的run(){ Log.i(LOGTAG计划); } } 私人LogTask mLogTask; @覆盖 公众的IBinder onBind(最终意向意图){ 返回null; } @覆盖 公共无效的onCreate(){ super.onCreate(); Log.v(StartServiceAtBoot,StartAtBootService创建); 玩家= MediaPlayer.create(这一点,R.raw.sirensound); player.setLooping(假); } 公共无效调用onStart(意向意图,诠释标志诠释startId){ Log.v(StartServiceAtBoot,StartAtBootService - onStartCommand()); player.start(); } @覆盖 公共无效的onDestroy(){ super.onDestroy(); Log.v(StartServiceAtBoot,StartAtBootService毁); }}
解决方案
在我的应用程序管理,当用户摇动设备,以创造良好的,而我的应用程序运行....
公共类SensorTest扩展活动实现SensorEventListener, OnCompletionListener {私人的SensorManager的SensorManager;私人布尔色= FALSE;私人长期LASTUPDATE;私人MediaPlayer的mMediaplayer;/ **当第一次创建活动调用。 * /@覆盖公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的SensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE); sensorManager.registerListener(这一点,的SensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER) SensorManager.SENSOR_DELAY_NORMAL); LASTUPDATE = System.currentTimeMillis的();}公共无效onSensorChanged(SensorEvent事件){ 如果(event.sensor.getType()== Sensor.TYPE_ACCELEROMETER){ 浮动[] =值event.values; 浮X =值[0]; 浮Y =值[1]; 浮Z =值[2]; 浮动accelationSquareRoot =(X * X + Y * Y + Z * Z) /(SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH); 长actualTime = System.currentTimeMillis的(); 如果(accelationSquareRoot&GT = 2)// { 如果(actualTime - LASTUPDATE< 200){ 返回; } LASTUPDATE = actualTime; 如果(彩色){ }其他{ 尝试{ AssetFileDescriptor AFD = getAssets()。openFd( gavel_single.wav); mMediaplayer =新的MediaPlayer(); mMediaplayer.setDataSource(afd.getFileDescriptor(),AFD .getStartOffset(),afd.getLength()); afd.close(); mMediaplayer prepare()。 mMediaplayer.start(); }赶上(例外五){ e.printStackTrace(); } } } }}公共无效onAccuracyChanged(传感器传感器,精度INT){ // TODO自动生成方法存根}@覆盖保护无效onResume(){ super.onResume(); //注册此类作为方向的监听器和 //加速度传感器 sensorManager.registerListener(这一点,的SensorManager .getDefaultSensor(Sensor.TYPE_ACCELEROMETER) SensorManager.SENSOR_DELAY_NORMAL);}@覆盖保护无效的onPause(){ //注销监听器 sensorManager.unregisterListener(本); super.onStop();}@覆盖保护无效的onDestroy(){ super.onDestroy(); 如果(mMediaplayer!= NULL){ mMediaplayer.release(); mMediaplayer = NULL; }}公共无效onCompletion(MediaPlayer的MP){ mp.seekTo(0); mp.release();}
}
I want to have a sound when my application is installed. I tried this by using broadcastreciever in my application. In the broadcast reciever iam running a service to start media player. But iam not able to get into on recieve method of the broadcast reciever. but if i try to install another app iam getting the event. how to get the event in my app only. My permissions in manifest file
<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>
and in the broadcast reciever
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));
}
}
}
and the service is
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");
}
}
解决方案
in my application I managed to create sound when a user shakes the device while my application is running....
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();
}
}
相关推荐
最新文章