【问题标题】:Android Service without any GUI没有任何 GUI 的 Android 服务
【发布时间】:2011-10-10 09:17:31
【问题描述】:

我想要一个没有任何 GUI 或活动的 android 应用程序。就我而言,我将只显示一条我要求的自定义 toast 消息。我给出了我的代码 sn-p,它显示完成但没有预期的结果。

清单文件是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="rit.utility"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
   <service android:enabled="true" android:name="MyService"></service> 
    <receiver android:enabled="true" android:name="MyIntentReceiver">       
    <intent-filter>         
    <action android:name="MY_INTENT" />       
    </intent-filter>     
    </receiver> 
</application>    

接收者类是

    public class MyIntentReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context _context, Intent _intent)
    {
        if(_intent.getAction().equals("MY_INTENT"))
        {
        _context.startService(new Intent(_context, MyService.class)); 
        }
    }
}

服务类是

    public class MyService extends Service
{
        private final IBinder mBinder = new MyBinder();
        public void onCreate()
        {
            super.onCreate();
            createToast();
        }
        public void createToast()
        {
             TextView textView = new TextView(this);
             textView.setBackgroundColor(Color.GRAY);
             textView.setTextColor(Color.BLUE);
             textView.setPadding(10,10,10,10);
             textView.setText("Textview as Toast");
             /** Create a Toast to display a View.
             * Here we are going to display a TextView.
             * Toast setView() is used to display a View.
             * Toast Display Duration is Long. So it will display for long time.
             * Toast setGravity() is used to set position to display the toast. */
             Toast toastView = new Toast(this);
             toastView.setView(textView);
             toastView.setDuration(Toast.LENGTH_LONG);
             toastView.setGravity(Gravity.CENTER, 0,0);
             toastView.show();
       }

        @Override
        public IBinder onBind(Intent intent)
        {
            // TODO Auto-generated method stub
            return null;
        }
        public class MyBinder extends Binder
        {
            MyService getService()
            {
                return MyService.this;
            }
        }

}

请帮助我显示自定义吐司在哪里出错???

【问题讨论】:

  • 在这里查看我的答案以获得自动启动应用程序,它将帮助您stackoverflow.com/questions/7690350/…
  • 谢谢,您的链接非常有用。我现在可以运行我的服务了。我的工作中有一个小错误。我的接收器类期待它的包路径。非常感谢:-)

标签: android class service android-activity


【解决方案1】:

有点晚了,但有人会发现它有用:

你不见了:

&lt;category android:name="android.intent.category.DEFAULT" /&gt;

所以你应该有

&lt;intent-filter&gt;

&lt;category android:name="android.intent.category.DEFAULT" /&gt;

&lt;action android:name="MY_INTENT" /&gt;

&lt;/intent-filter&gt;

【讨论】:

    【解决方案2】:

    您确定 MyService 已启动或创建的服务类吗?

    当你想启动服务时,你必须确保 MyIntentReceiver 被切换, 有一种方法可以尝试: 您可以在 Manifest.xml 中设置以下代码

     <receiver android:name=".MyReceiver">
             <intent-filter>
                <action android:name="android.intent.action.AIRPLANE_MODE" />
            </intent-filter>
     </receiver>
    

    运行您的应用程序;然后将您的设备更改为飞行模式,您会发现您的服务已创建;

    当然你可以用其他方式来切换接收器;

    【讨论】:

    • 这是我的问题。我的服务类被命名为“MyService”,您在代码 sn-p 中看到它应该由广播接收器类启动。我调试了广播接收器类,但代码流没有到达它可以提供服务的入口点。我是全新的,您能否建议是否有可能使用我的编码中的任何活动从广播接收器类启动服务。
    • 我改了答案,这个网站我不熟悉;
    【解决方案3】:

    清单更改 ::

    <service android:enabled="true" android:name="MyService"></service> 
        <receiver android:enabled="true" android:name="MyIntentReceiver">   
    

    <service android:enabled="true" android:name=".MyService"></service> 
        <receiver android:enabled="true" android:name=".MyIntentReceiver">   
    

    【讨论】:

    • 感谢您的回复,但应用程序仍然没有显示任何 toast 消息,这是服务启动后的预期。我认为服务没有启动。我想知道如何启动服务?
    • 不,我没有收到任何错误。但是告诉我一件事,这是否可以在没有任何 GUI 或活动的 android 中做任何事情?简单的应用程序将用作实用程序类。我希望你能明白我想要什么。
    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多