1

我想在类的 onHandleIntent()方法中打开一个自定义对话框IntentService,但是当我编写用于在此方法中显示 Dialog 的代码时,它会显示错误

The method findViewById(int) is undefined for the type MyAlarmService

谁能建议我如何解决这个问题?

我用过的代码:

public class MyAlarmService extends IntentService { 
public void onCreate() {
    super.onCreate();
}

public MyAlarmService() {
    super("MyAlarmService");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, startId, startId);
    Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
    return START_STICKY;
}
            
@SuppressWarnings("static-access")
@Override
protected void onHandleIntent(Intent intent) {
    final Dialog alarmDialog = new Dialog(MyAlarmService.this);
    alarmDialog .requestWindowFeature(alarmDialog.getWindow().FEATURE_NO_TITLE);
    alarmDialog .getWindow().setBackgroundDrawable(new ColorDrawable(0));

    Context mContext = getApplicationContext();
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.clear_all_warning_dialog, (ViewGroup) findViewById(R.id.layout_warning_dialog));
    
    TextView dialogTitile = (TextView) layout.findViewById(R.id.dialog_title_text);
    
    TextView dialogDesc = (TextView) layout.findViewById(R.id.dialog_desc_text);
    
    Button buttonYes = (Button) layout.findViewById(R.id.button_yes);               
    
    Button buttonNo = (Button) layout.findViewById(R.id.button_no);             

    alarmDialog.setContentView(layout);
    alarmDialog.show();
    
    buttonYes.setOnClickListener(new OnClickListener() {                    
        @Override
        public void onClick(View v) {
            alarmDialog.dismiss();
        }
    }); 
    buttonNo.setOnClickListener(new OnClickListener() {                 
        @Override
        public void onClick(View v) {
            alarmDialog.dismiss();
        }
    }); 
}
}
4

2 回答 2

4

只需启动一个活动即可完成,而不是考虑膨胀对话框:

@Override
protected void onHandleIntent(Intent intent) {
    synchronized (this) 
  {
    startActivity(new Intent(this,ActivityXYZ.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
  }
}

制作对话框布局(这是您在上面开始的 ActivityXYZ 的布局),如:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:orientation="vertical" >

<TextView
    android:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_margin="10dp"
    android:gravity="center_horizontal"
    android:text="Alert Dialog Message"
    android:textColor="#fff"
    android:textSize="16dp" >
</TextView>
<LinearLayout  android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginBottom="5dp">

<Button
    android:id="@+id/ButtonOk"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="Ok"
    android:textColor="#000"
    android:textSize="18sp"
    android:textStyle="bold" >
</Button>

<Button
    android:id="@+id/ButtonCancel"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="Cancel"
    android:textColor="#000"
    android:textSize="18sp"
    android:textStyle="bold"
   >
</Button>
</LinearLayout>
</LinearLayout>

并在清单文件中将其包含在该活动的配置中:

android:theme="@android:style/Theme.Dialog"

在为该活动制作布局时,只需添加文本视图和两个按钮,就像在 Dialogs 中所做的那样,它会产生 Dialog 从 Service 膨胀的效果。

如有任何澄清,请告诉我。:))))

于 2012-07-02T12:26:53.260 回答
0

我认为您不能在服务中膨胀视图,因为 IntentService 不在 UI 线程上运行,另一种解决方案是将您的服务绑定到您的活动并编写一个侦听器,该侦听器将显示对话框并将其注册到您的服务在 onBind() 方法中。

您可以通过以下方式扩展您的服务:

public class MyAlarmService extends IntentService {

    private MyListener mListener;

    // Your implementation here

    public void registerListener(MyListener listener){
        mListener = listener;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
                listener.postToUIThread();

        // Other things you might want to do
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyAlarmBinder(this);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
}

然后编写 binder 和 listener 如下:

public class MyListener {

    private Handler mHandler;
    private Runnable mRunnable;

    public MyListener(Handler handler, Runnable runnable){
        this.mHandler = handler;
        this.mRunnable = runnable;
    }

    public void postToUIThread(){
        mHandler.post(mRunnable);
    }
}

public class MyAlarmBinder extends Binder {
    private WeakReference<MyAlarmService> mService;

    public MyAlarmBinder(MyAlarmService service){
        mService = new WeakReference<MyAlarmService>(service);
    }

    public MyAlarmService getService(){
        return mService.get();
    }
}

请注意,在侦听器中,我使用在 UI 线程上创建的 Handler 将对话框创建代码发布到 UI 线程。

在您的主要活动中,您可以将它们放在一起:

private MyAlarmService mService;

private Handler mHandler = new Handler;
private Runnable mRunnable = new Runnable(){

    @Override
    public void run(){
        // Show dialog here
    }
}

private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName name, IBinder service) {
        mService = ((MyAlarmBinder)service).getService();
        mService.registerListener(new MyListener(mHandler, mRunnable));
    }

    public void onServiceDisconnected(ComponentName name) {
        mService = null;
    }
};

@Override
protected void onResume() {
    super.onResume();
    bindService(new Intent(this, MyAlarmService.class), mConnection,
        Context.BIND_AUTO_CREATE);
}

@Override
protected void onPause() {
    super.onPause();
    if(mConnection != null){
        unbindService(mConnection);
    }
}

我没有尝试过,但是稍作修改,这应该可以解决您的问题。如果您有任何其他问题,请随时提出。

于 2012-07-02T11:57:27.303 回答