【发布时间】: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