没有任何 GUI 的 Android 服务
我想要一个没有任何 GUI 或活动的 Android 应用程序。就我而言,我将仅显示我的要求的自定义 toast 消息。我给出了我的代码片段,显示已完成但没有预期的结果。
清单文件是
<?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;
}
}
}
请帮助我在哪里显示自定义 toast 时出错了???
I want an application in android without any GUI or activity. In my case, I will show just a custom toast message that is my requirement. I am giving my code snippet, that is showing done but with no desired result.
Manifest file is
<?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>
Receiver Class is
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));
}
}
}
Service class is
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;
}
}
}
please help me where am I making mistake for showing custom toast???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有点晚了,但有人会发现它有用:
你缺少:
所以你应该有
somewhat late but somebody can find it usefull:
You are missing:
<category android:name="android.intent.category.DEFAULT" />
so you should have
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="MY_INTENT" />
</intent-filter>
您确定 MyService 的服务类已启动或创建吗?
当您要启动服务时,必须确保 MyIntentReceiver 已切换,
有一种方法你可以尝试:
您可以在 Manifest.xml 中设置以下代码
运行您的应用程序;然后将您的设备更改为飞行模式,您会发现您的服务已创建;
当然你可以用另一种方式来切换接收器;
Do you sure service class that MyService is started or created??
when you want to start the service, you must make sure the MyIntentReceiver is toggled,
there is a way you can try :
you can set following code in you Manifest.xml
run your app; then change your device is Airplane mode, you will find your service is created;
of course you can use another way to toggle the receiver;
清单更改 ::
为
change in manifest ::
to