我正在尝试让我的IntentService显示Toast消息,但是当从onHandleIntent消息发送它时,toast显示但是卡住了屏幕并且从不离开.我猜它是因为onHandleIntent方法不会发生在主服务线程上,但是我怎么能移动呢?
有人有这个问题并解决了吗?
我创建的这个IntentService将在onStartCommand()和onDestroy()中显示Toasts,但不在onHandleIntent()中显示.我错过了一些关于IntentService限制的内容吗?
public class MyService extends IntentService {
private static final String TAG = "MyService";
public MyService(){
super("MyService");
}
@Override
protected void onHandleIntent(Intent intent) {
cycle();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); //This happens!
return super.onStartCommand(intent,flags,startId);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
Toast.makeText(this, "service stopping", Toast.LENGTH_SHORT).show(); //This happens!
super.onDestroy();
}
private void cycle(){
Toast.makeText(this, "cycle done", Toast.LENGTH_SHORT).show(); //This DOESN'T happen!
Log.d(TAG,"cycle completed"); //This happens!
} …Run Code Online (Sandbox Code Playgroud) 这是我必须显示Toast500毫秒的方式.虽然,它显示超过一秒钟.
Toast.makeText(LiveChat.this, "Typing", 500).show();
Run Code Online (Sandbox Code Playgroud)
我怎么才能显示Toast500毫秒?
Material设计网站提到了一个名为Snackbar的类似Toast的新元素:http://www.google.com/design/spec/components/snackbars-and-toasts.html
Android L预览SDK文档(抱歉无法链接,因为它只能下载)在类列表中没有提及Snackbar或者在Toast类文档中没有提到修饰符.我错过了一些明显的东西,还是应该建立自己的Snackbar.java?
我的Android应用程序显示几个Toast消息.我最近在运行Android 5.1.1的Galaxy S6上安装了它,并注意到消息最初显示在屏幕中心周围,然后它们移动到正确位置(如果没有指定重力,则接近底部),然后返回到在逐渐消失之前的初始位置.
Context context = getApplicationContext();
String newMsg = getString(R.string.wild_card_msg);
Toast mToast = Toast.makeText(context, newMsg, Toast.LENGTH_LONG);
mToast.setGravity(Gravity.CENTER, 0, 0);
mToast.show();
Run Code Online (Sandbox Code Playgroud)
更新:
有其他人遇到此问题,知道如何解决或知道解决方法吗?
请注意,我接受了Nick的回答,建议使用snackBar作为解决方法.
我只是打电话给我Activity:
Toast.makeText(this, "This is a toast", Toast.LENGTH_SHORT).show()
Run Code Online (Sandbox Code Playgroud)
但结果是在toast容器的顶部对齐了一个文本,而不是它应该在内部居中:

关于什么可能出错的任何想法?
更新: 我不同意这是一个重复 - 因为我正在寻找一种方法退出主应用程序,仍然显示从服务Toast.
在一个非常简单的测试应用程序中我有2个按钮:

单击任何按钮将运行带有相应操作字符串的服务("打开"或"闪烁") -
public class OpenActivity extends Activity {
private Intent mServiceIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
mServiceIntent = new Intent(this, RegionService.class);
}
public void openCar(View v) {
mServiceIntent.setAction("open");
startService(mServiceIntent);
}
Run Code Online (Sandbox Code Playgroud)
public class RegionService extends IntentService {
private static final String TAG = "RegionService";
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "Received an intent: " + intent);
String action = intent.getAction();
Log.d(TAG, "Received an action: " + …Run Code Online (Sandbox Code Playgroud) 我们的目标是在发生呼号时展示祝酒词.当设备被锁定并发生接收呼叫时,这将不起作用.然后在" 锁定的全屏幕进入呼叫视图" 后面可以看到吐司.
我们尝试了不同的approches,结果相同:
允许:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Run Code Online (Sandbox Code Playgroud)
PhoneCallListener的设置:
public class PhoneCallDetector : PhoneStateListener
{
public override void OnCallStateChanged(CallState state, string incomingNumber)
{
ShowToast(incomingNumber);
base.OnCallStateChanged(state, incomingNumber);
}
private void ShowToast(string phonenumber)
{
Toast toast = Toast.MakeText(Application.Context, phonenumber, ToastLength.Long);
toast.SetGravity(GravityFlags.Center, 0, 0);
toast.Show();
}
}
Run Code Online (Sandbox Code Playgroud)
我们知道一些可以通过" 锁定的全屏嵌入调用视图"成功显示toast的应用程序,但是它们是用java编写的......它们也没有做任何特殊的事情,然后Toast.MakeText(....).
编辑: => PhoneStateListener在后台生命.从服务开始.
服务如何开始?
Intent serviceStart = new Intent(context, typeof(PhoneCallService));
context.StartService(serviceStart);
Run Code Online (Sandbox Code Playgroud)
如何调用PhoneCallDetector?
var phoneCallDetector = m_scope.Resolve<PhoneCallDetector>();
var tm = (TelephonyManager)GetSystemService(TelephonyService);
tm.Listen(phoneCallDetector, PhoneStateListenerFlags.CallState);
Run Code Online (Sandbox Code Playgroud)
谢谢你帮助我:-)
c# xamarin.android android-toast xamarin.forms android-phone-call
我设置了吐司的重力,使其显示在屏幕顶部,代码如下\xef\xbc\x9a
\nToast toast = Toast.makeText(getActivity(), "\xe9\x82\xae\xe7\xae\xb1\xe5\x9c\xb0\xe5\x9d\x80\xe4\xb8\x8d\xe8\x83\xbd\xe4\xb8\xba\xe7\xa9\xba\xef\xbc\x81", Toast.LENGTH_SHORT);\ntoast.setGravity(Gravity.TOP, 0, 0);\ntoast.show();\nRun Code Online (Sandbox Code Playgroud)\n它在带有 LogCat 消息的 AVD Nexus 6 API 30 中不起作用:
\nE/Toast: setGravity() shouldn't be called on text toasts, the values won't be used\nRun Code Online (Sandbox Code Playgroud)\n但是,当我在 Android 9 的诺基亚 X6 手机上安装该 apk 时,似乎没问题。\n请帮助我!
\n我想编写一个方法,只对参数采用某些值,比如ToastAndroid 中的类中的fe .您只能使用Toast.LENGTH_SHORT或Toast.LENGTH_LONG作为方法的持续时间makeText(Context context, int resId, int duration).我查看了Toast该类的源代码,但没有发现任何内容.我怎样才能做到这一点?
android-toast ×10
android ×9
java ×2
toast ×2
c# ×1
galaxy ×1
methods ×1
parameters ×1
service ×1