我想显示一个通知,当用户点击它时,应该播放一个声音文件。
在Android Studio中,我已将文件test.mp3
复制到文件夹app\res\raw
中。通知是通过以下代码发出的:
Resources resources = getResources();
Uri uri = new Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(resources.getResourcePackageName(R.raw.test))
.appendPath(resources.getResourceTypeName(R.raw.test))
.appendPath(resources.getResourceEntryName(R.raw.test))
.build();
Intent playSoundIntent = new Intent();
playSoundIntent.setAction(android.content.Intent.ACTION_VIEW);
playSoundIntent.setDataAndType(uri, "audio/*");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
playSoundIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
MainActivity.notificationChannelId)
.setSmallIcon(android.R.drawable.ic_media_play)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText("Tap to play sound!")
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(12345678, builder.build());
它无法正常工作。显示该通知,如果我点击它,它会消失(由于setAutoCancel(true)
)。但是我听不到声音。为什么?
我该如何调试?
非常感谢您!
答案 0 :(得分:0)
我通过创建这样的意图来管理它:
<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "odcinki";
$conn = mysqli_connect($host,$user,$password,$dbname);
if (mysqli_connect_errno()) {echo "Brak polaczenia" . mysqli_connect_error();}
$postID = get_queried_object_id();
$current_user = wp_get_current_user();
$current= $current_user->ID ;
$sql = "SELECT lastname FROM `$current` WHERE id = '$postID'";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$keepchecked= $row['lastname'];
}
?>
<?php if ( is_user_logged_in() ) { ?> Status: <input type="checkbox" name = "seks" value ="1" id="checkboxtest" <?php echo ($keepchecked == "YES") ? 'checked="checked"' : ''; ?> >
<?php
}
?>
<script type="text/javascript">
$(document).ready(function(e) {
$('#checkboxtest').change(function(){
var checkboxstatus = "";
var postID = "<?php echo $postID;?>";
if( $('#checkboxtest').prop('checked') )
{checkboxstatus = "YES";}
else
{checkboxstatus = "NO";}
$.ajax({
type: "POST",
url: "checkboxtestbackend.php",
data: {checkboxstatus: checkboxstatus, postID : postID},
success: function(result) {
console.log('the data was successfully sent to the server');
}
})
});
});
?>
并添加活动:
Intent playSoundIntent = new Intent(this, PlaySoundActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, playSoundIntent, 0);
尽管这可行,但是我仍然对为什么前一种方法行不通感兴趣。我已经看到很多类似的代码片段。而且我仍然想知道如何调试以前的方法来理解错误。有人可以帮我吗?