activity.runOnUiThread()内的run()方法没有被执行

本文探讨了在Android开发中,使用runOnUiThread方法时遇到的不执行问题。关键在于确保调用该方法的对象实例为Activity类型,否则将抛出ClassCastException。文章提供了检查和判断的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

activity.runOnUiThread(new Runnable() {
                public void run()
                {
                    Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
                }
            });

run()方法没有执行,查找原因,在原来,掉用runOnUiThread的activity,一定得是Activity对象,so,在使用之前,需要增加判读:
if(activity instanceOf Activity){

}

参考链接


原文:

It doesn't always work, you have to be sure that what you are casting is
effectively an Activity or you'll have a ClassCastException. To do that you can do
"if(context instanceOf Activity){ // proceed to cast }"

 

 

转载于:https://www.cnblogs.com/butterfly-clover/p/6410792.html

``` package com.example.mp3; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Build; import android.os.IBinder; import android.util.Log; import androidx.core.app.NotificationCompat; public class MusicService extends Service { private MediaPlayer mediaPlayer; private static final String CHANNEL_ID = "MusicServiceChannel"; runOnUiThread(new Runnable() { public void run() { showNotification("Music Playing"); } }); @Override public void onCreate() { super.onCreate(); mediaPlayer = MediaPlayer.create(this,R.raw.music1); if(mediaPlayer == null){ Log.e("MusicService","Failed to load mp3 file"); }else{ mediaPlayer.setLooping(false); } createNotificationChannel(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { String action = intent.getAction(); if (action != null) { switch (action) { case "开始播放": startPlayback(); break; case "停止播放": stopPlayback(); break; } } return START_NOT_STICKY; } private void startPlayback() { new Thread(() -> { if (mediaPlayer == null) { mediaPlayer = MediaPlayer.create(this, R.raw.music1); mediaPlayer.setLooping(false); } if (!mediaPlayer.isPlaying()) { mediaPlayer.start(); Activity.runOnUiThread(() -> showNotification("Music Playing")); } }).start(); } private void stopPlayback() { new Thread(() -> { if (mediaPlayer != null) { if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); } mediaPlayer.reset(); mediaPlayer.release(); mediaPlayer = null; } Activity.runOnUiThread(() -> showNotification("Music Stopped")); }).start(); } @SuppressLint("ForegroundServiceType") private void showNotification(String status) { Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("Music Player") .setContentText(status) .setSmallIcon(R.drawable.musicphoto) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .build(); startForeground(1, notification); } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( CHANNEL_ID, "Music Service Channel", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(channel); } } @Override public void onDestroy() { super.onDestroy(); if (mediaPlayer != null) { mediaPlayer.release(); mediaPlayer = null; } } @Override public IBinder onBind(Intent intent) { return null; } }```runOnUiThread报错
最新发布
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值