use*_*764 2 android toast android-service android-notifications android-alertdialog
我有一个监视前台应用程序的后台服务。它用于保护某个应用程序,这样,如果受保护的应用程序在前台,然后由于另一个应用程序启动而被推送到后台,它将向用户显示另一个应用程序已接管前台应用程序的通知。
我能够检测到该开关,但是有什么方法可以显示Toast通知/ AlertDialog,以便在检测到该警报后在服务中提醒用户?
您可以在Service课堂上获取应用基础上下文,并使用来显示祝酒词Handler。
private Context appContext;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
appContext=getBaseContext();//Get the context here
}
//Use this method to show toast
void showToast(){
if(null !=appContext){
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run()
{
Toast.makeText(appContext, "Toast Message", Toast.LENGTH_SHORT)show();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4333 次 |
| 最近记录: |