2012-03-12 76 views
0

我是一个新的android,所以我试图开发一个应用程序,在特定的时间之后向用户显示通知,我不知道:是否可以在服务中使用对话框! !或不...是否可以在Service [android app]中显示对话框?

我的目标是:要问他后特定时间的问题,因为在我的代码如下所示,

import android.app.AlertDialog; 
import android.app.Service; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.IBinder; 
import android.widget.Toast; 

public class MyAlarmService extends Service { 

    @Override 
    public void onCreate() { 
     Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG) 
       .show(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG) 
       .show(); 
     return null; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG) 
       .show(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 
     Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG) 
       .show(); 
     // For Notification 
     final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
     alertDialog.setTitle("Conformation"); 
     alertDialog 
       .setMessage("Are you sure you want to Expand Report Region?"); 
     alertDialog.setButton("Yes", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // Sending a Message to server that the plaintiff wants to 
       // expand report region 
      } 
     }); 
     alertDialog.setButton2("No", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // Do nothing 
      } 
     }); 
    } 
} 

此外,我不知道我将使用服务或广播接收器? ?

任何建议,提前感谢=)

回答

0

它总是更好地使用广播接收器与服务并重。在广播接收器中注册意图并从广播接收器重定向请求到服务。

它可能显示服务的对话框。只有你需要的是一个活动。

总之,您的应用程序将有一个活动,一个服务和一个广播接收器。您的广播接收器将捕获将重定向到服务的意图,然后您的服务将调用显示对话框的活动。

广播接收机 - >服务 - >活动

+0

谢谢你,但是我没有把你好,请你多解释一下,谢谢。 =') – Samiah 2012-03-12 16:54:44

+0

如果我创建一个活动,然后调用broadcastRecevier,并调用服务如何使服务调用基本活动+我怎样才能让活动显示dialogAlert,而无需按任何按钮,! – Samiah 2012-03-12 17:01:40

+0

不是你的活动会打电话给BroadcastReciever。 BroadcastReceiver将由Android调用。然后从您的广播接收器调用服务和服务将调用您的活动。详细了解广播接收者的工作原理 – 2012-03-15 06:53:45

0

广播接收机 - >服务 - >活动,这种序列是良好的。 用于在不按任何按钮调用方法的情况下显示警报对话框,在Activity的onCreate()方法中显示showDialog()。

相关问题