android封装全局调用的toast_安卓全局toast

使用全局toast,防止toast因为消息过多,弹个不停

public class CustomToast {

private static int mDuration = 5000;//toast默认的时间

private static Toast mToast;

private static Handler mHandler = new Handler();

private static Runnable r = new Runnable() {

public void run() {

mToast.cancel();

}

};

public static void showToast(Context mContext, String text) {

showToast(mContext, text, mDuration);

}

public static void showToast(Context mContext, int resId) {

showToast(mContext, mContext.getResources().getString(resId), mDuration);

}

public static void showToast(Context mContext, String text, int duration) {

mHandler.removeCallbacks(r);

if (mToast != null)

mToast.setText(text);

else

mToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG);

mHandler.postDelayed(r, duration);

mToast.show();

}

public static void showToast(Context mContext, int resId, int duration) {

showToast(mContext, mContext.getResources().getString(resId), duration);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android 中实现跨进程调用 Toast,您需要使用 AIDL(Android 接口定义语言)来定义一个接口,该接口将允许其他应用程序与您的应用程序进行通信。然后,您可以在您的应用程序中实现此接口,并将其公开为服务。其他应用程序可以绑定到您的服务,并使用该接口调用 Toast 方法,从而在您的应用程序中显示 Toast。 以下是一个简单的示例代码: 1. 在您的应用程序中创建一个 AIDL 文件,例如 IToast.aidl,其中包含以下内容: ``` interface IToast { void showToast(String message); } ``` 2. 实现该接口并将其公开为服务: ``` public class ToastService extends Service { private final IBinder mBinder = new ToastBinder(); @Override public IBinder onBind(Intent intent) { return mBinder; } private void showToast(String message) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); } private class ToastBinder extends IToast.Stub { @Override public void showToast(String message) { ToastService.this.showToast(message); } } } ``` 3. 在您的应用程序中启动服务: ``` Intent intent = new Intent(this, ToastService.class); startService(intent); ``` 4. 在其他应用程序中绑定到您的服务,并使用 AIDL 接口调用 showToast 方法: ``` private IToast mToastService; private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mToastService = IToast.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { mToastService = null; } }; private void showToast(String message) { try { if (mToastService != null) { mToastService.showToast(message); } } catch (RemoteException e) { e.printStackTrace(); } } ``` 请注意,为了使此代码正常工作,您需要确保您的应用程序和其他应用程序都具有相应的权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值