An intent for an Activity (RluSvcMenu) is fired from a Service when a user clicks on the Notification item. RluSvcMenu then successfully displays a custom dialog box with three option buttons (Copy/Next; Next; Quit). All is well, except that when any of the three buttons is clicked, the Listener function does not seem to be called. Here are the pertinent parts of the RluSvcMenu code:
package com.lovelady.android.RluApp;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class RluSvcMenu extends Activity implements OnClickListener {
private int action;
private Button btnCopyNext;
private Button btnNext;
private Button btnQuit;
private final int SVC_MENU_DIALOG = 1;
private Dialog dialog;
private final String MY_TAG = "RluSvcMenu";
private final String MESSAGE1 = "onCreate message";
private final String MESSAGE2 = "onPrepare message";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showDialog(SVC_MENU_DIALOG);
Log.d(MY_TAG, "showDialog() has completed.");
}
protected Dialog onCreateDialog(int id) {
dialog = new Dialog(this);
dialog.setContentView(R.layout.rlu_service_menu);
dialog.setTitle("RLU_APP");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(MESSAGE1);
ImageView image = (ImageView) dialog.findViewById(R.id.icon);
image.setImageResource(R.drawable.ic_rlu);
btnCopyNext = (Button) dialog.findViewById(R.id.BtnCopyNext);
btnCopyNext.setOnClickListener(this);
btnNext = (Button) dialog.findViewById(R.id.BtnNext);
btnNext.setOnClickListener(this);
btnQuit = (Button) dialog.findViewById(R.id.BtnQuit);
btnQuit.setOnClickListener(this);
return dialog;
}
public void onClick(View v) {
action = v.getId() ;
Toast.makeText(getApplicationContext()
, "Button " + action + " clicked."
, Toast.LENGTH_SHORT).show();
Log.d(MY_TAG, "Button " + action + " clicked.");
dialog.dismiss();
}
protected void onPrepareDialog(int id, Dialog dialog) {
dialog.setContentView(R.layout.rlu_service_menu);
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(MESSAGE2);
ImageView image = (ImageView) dialog.findViewById(R.id.icon);
image.setImageResource(R.drawable.ic_rlu_app);
}
}
The XML to build the “service menu” is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
>
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/icon"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textColor="#FFF"
/>
<Button android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:layout_toRightOf="@+id/icon"
android:layout_width="wrap_content"
android:id="@+id/BtnCopyNext"
android:text="Copy/Next"></Button>
<Button android:layout_height="wrap_content"
android:layout_toRightOf="@+id/BtnCopyNext"
android:layout_below="@+id/text"
android:layout_width="wrap_content"
android:id="@+id/BtnNext"
android:text="Next"></Button>
<Button android:layout_height="wrap_content"
android:layout_toRightOf="@+id/BtnNext"
android:layout_below="@+id/text"
android:layout_width="wrap_content"
android:id="@+id/BtnQuit"
android:text="Quit"></Button>
</RelativeLayout>
… and finally, the AndroidManifest.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lovelady.android.AppName"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:label="@string/app_name"
android:icon="@drawable/ic_mainIcon">
<activity android:name=".RluSvcMenu"></activity>
<activity android:name=".RluOverview"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- enable the search dialog to send searches to RluSearch -->
<meta-data android:name="android.app.default_searchable"
android:value=".RluSearch" />
</activity>
<activity android:name=".RluDetails"></activity>
<activity android:name=".RluImportFile"></activity>
<activity android:name=".RluImportFilteredFile"></activity>
<activity android:name=".RluSearch">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<service android:name=".RluManager"
android:exported="false"
android:label="@string/service_name">
</service>
</application>
</manifest>
What might cause touches to not be delivered to the Activity? Are they going to the service instead? If so, what can I do to get focus? (I thought I’d get focus by putting up the dialog, but the only clue I have, from logcat, indicates:
07-09 21:04:20.060 I/InputDispatcher( 302): Delivering touch to current input \
target: action: 0, channel '409079b8 Rluapp (server)'
(where “Rluapp” is the value of @string/my_app, and the name of the package)
… or what hole did I forget to fill in my Listener? For all intents and purposes “nothing” happens when the buttons are pressed.
Hmmm, I suspect the dialog is running in a different thread? Well anyway if you use one and alert dialog they have DialogInterface.onClickListener class that handles that, there may be a better way to overcome the problem, but changing the code to and actual custom dialog fixes this.
Here is your original code modified with that fix, this is probably better anyway since you can then put the dialog in it’s own file/class etc.