- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个由通过 BLE 接收的消息控制的 Android 音乐播放器。我的应用程序在 API 4.4(18) 上运行良好,但在 8.1(27) 中崩溃可能是由于线程处理不当。这是我的扫描 Activity 和音乐播放器 Activity 以及来自 LogCat 的错误
扫描 Activity
public class MainActivity extends Activity {
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;
private static final long SCAN_PERIOD = 3000;
private Dialog mDialog;
public static final int permconst=7;
public static List<BluetoothDevice> mDevices = new ArrayList<BluetoothDevice>();
public static MainActivity instance = null;
private View mPermissionRationale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
checkperms();
if (!getPackageManager().hasSystemFeature(
PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "Ble not supported", Toast.LENGTH_SHORT)
.show();
finish();
}
final BluetoothManager mBluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Ble not supported", Toast.LENGTH_SHORT)
.show();
finish();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
ImageButton btn = (ImageButton) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
scanLeDevice();
showRoundProcessDialog(MainActivity.this, R.layout.loading_process_dialog_anim);
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
Intent deviceListIntent = new Intent(getApplicationContext(),
Device.class);
startActivity(deviceListIntent);
mDialog.dismiss();
}
}, SCAN_PERIOD);
}
});
//scanLeDevice();
showRoundProcessDialog(MainActivity.this, R.layout.loading_process_dialog_anim);
Timer mTimer = new Timer();
mTimer.schedule(new TimerTask() {
@Override
public void run() {
Intent deviceListIntent = new Intent(getApplicationContext(),
Device.class);
startActivity(deviceListIntent);
mDialog.dismiss();
}
}, SCAN_PERIOD);
instance = this;
}
public void showRoundProcessDialog(Context mContext, int layout) {
DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME
|| keyCode == KeyEvent.KEYCODE_SEARCH) {
return true;
}
return false;
}
};
mDialog = new AlertDialog.Builder(mContext).create();
mDialog.setOnKeyListener(keyListener);
mDialog.show();
// 娉ㄦ��姝ゅ��瑕���惧��show涔���� ������浼���ュ��甯�
mDialog.setContentView(layout);
}
private void scanLeDevice() {
//checkperms();
// new Thread() {
final Handler handler =new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// if(Build.VERSION.SDK_INT>21){
// mBluetoothAdapter.getBluetoothLeScanner().startScan(mleScanCallback);}
mBluetoothAdapter.startLeScan(mLeScanCallback);
}
},4000);
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
// new Thread(){
@Override
public void run() {
if (device != null) {
if (mDevices.indexOf(device) == -1)
mDevices.add(device);
}
}
});
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT
&& resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
super.onDestroy();
//scanLeDevice(false);
mDevices.clear();
System.exit(0);
}
private void checkperms(){
if(Build.VERSION.SDK_INT>19) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "permissions not granted", Toast.LENGTH_SHORT)
.show();
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION))
Toast.makeText(this, "pls grant permissions", Toast.LENGTH_SHORT)
.show();
else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
permconst);
return;}
}}}}
音乐播放器:
public int[] bname=new int[]{R.drawable.playinv, R.drawable.pausex, R.drawable.pauseinv, R.drawable.playx,
R.drawable.nextinv, R.drawable.nextx, R.drawable.previousinv, R.drawable.previousx, R.drawable.vdowninv,
R.drawable.vdownx, R.drawable.vupinv, R.drawable.vupx};
private Map<UUID, BluetoothGattCharacteristic> map = new HashMap<UUID, BluetoothGattCharacteristic>();
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName,
IBinder service) {
mBluetoothLeService = ((RBLService.LocalBinder) service)
.getService();
if (!mBluetoothLeService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
finish();
}
// Automatically connects to the device upon successful start-up
// initialization.
final Handler handler =new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothLeService.connect(mDeviceAddress);
}
},2000);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (RBLService.ACTION_GATT_DISCONNECTED.equals(action)) {
} else if (RBLService.ACTION_GATT_SERVICES_DISCOVERED
.equals(action)) {
getGattService(mBluetoothLeService.getSupportedGattService());
} else if (RBLService.ACTION_DATA_AVAILABLE.equals(action)) {
displayData(intent.getByteArrayExtra(RBLService.EXTRA_DATA));
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_player);
checkperms();
vup= findViewById(R.id.vup);
vdown=findViewById(R.id.vdown);
next =findViewById(R.id.next);
previous= findViewById(R.id.previous);
playpause =findViewById(R.id.plpau);
info=findViewById(R.id.textView);
tload=findViewById(R.id.load);
audioManager=(AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
Toast.makeText(getApplicationContext(),"Android Version: "+Build.VERSION.SDK_INT, Toast.LENGTH_LONG).show();
// mediaPlayer = MediaPlayer.create(getApplicationContext(), tracks[current]);
tload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getSongs();
MUSIC_LOADED=1;
info.setText(songT[current]);
Uri trackUri = ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,songIDS[current]);
mediaPlayer=MediaPlayer.create(getApplicationContext(), trackUri);
}
});
playpause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (pp) {
mediaPlayer.start();
playpause.setBackgroundResource(R.drawable.pausex);
pp = FALSE;
} else {
mediaPlayer.pause();
playpause.setBackgroundResource(R.drawable.playx);
pp = TRUE;}
}});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextTrack();
} });
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
previousTrack();
} });
vdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
volumeDown(audioManager);
}});
vup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
volumeUp(audioManager); }});
Intent intent = getIntent();
mDeviceAddress = intent.getStringExtra(Device.EXTRA_DEVICE_ADDRESS);
mDeviceName = intent.getStringExtra(Device.EXTRA_DEVICE_NAME);
// getActionBar().setTitle(mDeviceName);
// getActionBar().setDisplayHomeAsUpEnabled(true);
Intent gattServiceIntent = new Intent(this, RBLService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
//mediaPlayer.stop();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
mBluetoothLeService.disconnect();
mBluetoothLeService.close();
// mediaPlayer.stop();
System.exit(0);
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(mGattUpdateReceiver);
//mediaPlayer.stop();
}
@Override
protected void onDestroy() {
super.onDestroy();
mBluetoothLeService.disconnect();
mBluetoothLeService.close();
//mediaPlayer.stop();
System.exit(8);
}
private void displayData(byte[] byteArray) {
if (MUSIC_LOADED==1){
if (byteArray != null) {
String data = new String(byteArray);
x= data.substring (data.length()-1);
//Toast.makeText(getApplicationContext(),x,Toast.LENGTH_SHORT).show();
if (x.equals("1")){
selector=10;
colorChange(vup,selector);
Toast.makeText(getApplicationContext(),"Infitex_keypress_001"+"\n"+ "Volume up",Toast.LENGTH_SHORT).show();
volumeUp(audioManager);
}
if (x.equals("3")){
selector=4;
colorChange(next,selector);
Toast.makeText(getApplicationContext(),"Infitex_keypress_003"+"\n"+ "Track Advance",Toast.LENGTH_SHORT).show();
nextTrack();
}
if (x.equals("5") ){
if (pp){
selector=0;} else selector= 2;
colorChange(playpause,selector);
Toast.makeText(getApplicationContext(),"Infitex_keypress_005"+"\n"+ "Play/Pause",Toast.LENGTH_SHORT).show();
playPause(playpause);}
if(x.equals("7")){
selector=6;
colorChange(previous,selector);
Toast.makeText(getApplicationContext(),"Infitex_keypress_007"+"\n"+ "Track Reverse",Toast.LENGTH_SHORT).show();
previousTrack();
}
if(x.equals("9")){
selector=8;
colorChange(vdown,selector);
Toast.makeText(getApplicationContext(),"Infitex_keypress_009"+"\n"+ "Volume Down",Toast.LENGTH_SHORT).show();
volumeDown(audioManager);
}
}}}
private void getGattService(BluetoothGattService gattService) {
if (gattService == null)
return;
BluetoothGattCharacteristic characteristic = gattService
.getCharacteristic(RBLService.UUID_BLE_SHIELD_TX);
map.put(characteristic.getUuid(), characteristic);
BluetoothGattCharacteristic characteristicRx = gattService
.getCharacteristic(RBLService.UUID_BLE_SHIELD_RX);
mBluetoothLeService.setCharacteristicNotification(characteristicRx,
true);
mBluetoothLeService.readCharacteristic(characteristicRx);
}
private static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(RBLService.ACTION_GATT_CONNECTED);
intentFilter.addAction(RBLService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(RBLService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(RBLService.ACTION_DATA_AVAILABLE);
return intentFilter;
}
void nextTrack(){
mediaPlayer.pause();
current+=1;
if (current==5){
current=0;
}
getURI();
info.setText(songT[current]);
playpause.setBackgroundResource(R.drawable.pausex);
mediaPlayer.start();
pp=FALSE;
}
void previousTrack(){
mediaPlayer.pause();
if (current==0){
current=4;}
else {
current -= 1;
}
getURI();
info.setText(songT[current]);
playpause.setBackgroundResource(R.drawable.pausex);
mediaPlayer.start();
pp=FALSE;
}
public void playPause(ImageButton imageButton){
if (pp) {
mediaPlayer.start();
imageButton.setBackgroundResource(R.drawable.pausex);
pp = FALSE;
} else if(pp==FALSE) {
mediaPlayer.pause();
imageButton.setBackgroundResource(R.drawable.playx);
pp = TRUE;
}
}
public void volumeDown(AudioManager audioManager){
audioManager.adjustVolume(AudioManager.ADJUST_LOWER,AudioManager.FLAG_PLAY_SOUND);
}
public void volumeUp(AudioManager audioManager){
audioManager.adjustVolume(AudioManager.ADJUST_RAISE,AudioManager.FLAG_PLAY_SOUND);
}
public void colorChange(final ImageButton imageButton, final int name){
final int last= name+1;
new CountDownTimer(1500, 1000) {
public void onTick(long millisUntilFinished) {
imageButton.setBackgroundResource(bname[name]);
}
public void onFinish() {
imageButton.setBackgroundResource(bname[last]);
}
}.start();
}
// public void bCheck(ImageButton imageButton){
public void getSongs(){
int i=0;
ContentResolver contentResolver=getContentResolver();
Uri uri= EXTERNAL_CONTENT_URI;
Cursor songcursor= contentResolver.query(uri,null,null,null,null);
if (songcursor!=null && songcursor.moveToFirst()) {
int songtitle=songcursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songID=songcursor.getColumnIndex(MediaStore.Audio.Media._ID);
do{
songT[i] = songcursor.getString(songtitle);
songIDS[i]=songcursor.getLong(songID);
i+=1;
}
while(songcursor.moveToNext());
songcursor.close();
}}
public void getURI(){
Uri trackUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,songIDS[current]);
mediaPlayer=MediaPlayer.create(getApplicationContext(), trackUri);
}
private void checkperms(){
//Toast.makeText(this,"version", Toast.LENGTH_LONG);
if(Build.VERSION.SDK_INT>19) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(getApplicationContext(), "permissions not granted", Toast.LENGTH_SHORT)
.show();
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE))
Toast.makeText(getApplicationContext(),"pls grant permissions", Toast.LENGTH_SHORT)
.show();
else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MainActivity.permconst);
}
}}}
和 logcat:
com.example.parlatas.messages I/AndroidRuntime: VM exiting with result code 0, cleanup skipped.
I/WindowManager: WIN DEATH: Window{9297a52 u0 com.example.parlatas.messages/com.example.parlatas.messages.MainActivity}I/ActivityManager: Process com.example.parlatas.messages (pid 8530) has died: fore TOP W/ActivityManager:强制删除 ActivityRecord{b15576d u0 com.example.parlatas.messages/.MusicPlayer t245}:app 已死,无保存状态 W/InputDispatcher: channel 'd7b3552 com.example.parlatas.messages/com.example.parlatas.messages.MusicPlayer (server)' ~ 消费者关闭输入 channel 或发生错误。事件=0x9 E/InputDispatcher: channel 'd7b3552 com.example.parlatas.messages/com.example.parlatas.messages.MusicPlayer (server)' ~ Channel 已不可恢复地损坏,将被处理掉! I/WindowManager: WIN DEATH: Window{d7b3552 u0 com.example.parlatas.messages/com.example.parlatas.messages.MusicPlayer} W/InputDispatcher:试图注销已经注销的输入 channel “d7b3552 com.example.parlatas.messages/com.example.parlatas.messages.MusicPlayer(服务器)” W/NotificationService: 对象死于试图隐藏通知 android.app.ITransientNotification$Stub$Proxy@c10b00c 在包 com.example.parlatas.messages
我有单独的类,其中包含 BLE 常量和由扫描 Activity (主 Activity )填充的蓝牙设备列表
最佳答案
如果您的目标 API 级别高于 22,请尝试在 list 中添加以下权限。如果您要使用蓝牙硬件,新版本需要粗略定位权限(访问蜂窝位置信息)。它没有意义,但它需要新的 API 级别。
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-feature android:name="android.hardware.bluetooth_le"
那么要么您必须授予权限,要么应该以较低的 API 级别为目标。
关于android - 通过 BLE 的音乐播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50785288/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!