Android的服务展现敬酒 [英] Android Service to show toast

查看:178
本文介绍了Android的服务展现敬酒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code应该使用服务来显示敬酒消息。有没有错误,但它不显示敬酒。

主要活动

 公共类MainActivity延伸活动{

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    意图I =新的意图(这一点,BackgroundMusic.class);
    this.startService(ⅰ);

}



}
 

服务(其所谓的背景音乐,但现在它是为了显示敬酒消息)

 公共类BackgroundMusic扩展IntentService {

 公共BackgroundMusic(){
      超级(BackgroundMusic);
  }



 @覆盖
  保护无效onHandleIntent(意向意图){
      //通常我们会做一些工作在这里,就像要下载的文件。
      //对于我们的示例,我们只是睡,持续5秒。
     上下文的背景下= getApplicationContext();
     CharSequence的文字=你好干杯!;
     INT持续时间= Toast.LENGTH_SHORT;

     吐司面包= Toast.makeText(背景,文本,持续时间);
     toast.show();
 }



}
 

清单

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.example.starwars
安卓版code =1
机器人:VERSIONNAME =1.0>

<用途-SDK
    安卓的minSdkVersion =8
    机器人:targetSdkVersion =18/>

<应用
    机器人:allowBackup =真
    机器人:可调试=真
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
     <服务机器人:名称=。BackgroundMusic/>
    <活动
        机器人:名称=com.example.starwars.MainActivity
        机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>

            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动机器人:标签=@字符串/ APP_NAME机器人:NAME =BackgroundMusic/>
< /用途>

< /舱单>
 

解决方案

查看这部分文档的

  

(一IntentService有一些限制:

     

它不能直接与您的用户界面交互。为了把它   结果在用户界面中,你必须把它们发送到一个活动。

您需要把它放在主。请参阅<一href="http://stackoverflow.com/questions/3296639/toast-created-in-an-intentservice-never-goes-away#5420929">the答案在这里用的一种方式罗尼做到这一点。

和从上IntentService 完整文档

  

处理依次在每个意图使用辅助线程

This code is supposed to use a service to show a toast message. There are no errors, but it doesn't show the toast.

main activity

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent i= new Intent(this, BackgroundMusic.class);
    this.startService(i); 

}



}

service (its called Background Music but for now it is supposed to show a toast message)

public class BackgroundMusic extends IntentService {

 public BackgroundMusic() {
      super("BackgroundMusic");
  }



 @Override
  protected void onHandleIntent(Intent intent) {
      // Normally we would do some work here, like download a file.
      // For our sample, we just sleep for 5 seconds.
     Context context = getApplicationContext();
     CharSequence text = "Hello toast!";
     int duration = Toast.LENGTH_SHORT;

     Toast toast = Toast.makeText(context, text, duration);
     toast.show();
 }



}

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.starwars"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:debuggable="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
     <service android:name=".BackgroundMusic" />
    <activity
        android:name="com.example.starwars.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:label="@string/app_name" android:name="BackgroundMusic"/>
</application>

</manifest>

解决方案

See this part of the docs

(An IntentService has a few limitations:

It can't interact directly with your user interface. To put its results in the UI, you have to send them to an Activity.

You need to put it on the main Thread. See the answer here by rony of a way to do that.

and from the full documentation on IntentService

handles each Intent in turn using a worker thread

这篇关于Android的服务展现敬酒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆