Service warning popup dialog in android

I am trying to create an activity study application. As a starting point, I used an example of activity recognition from Android developers. In this example, when the user starts moving, a notification appears asking you to turn on the GPS. I am trying to replace this notification with an alarm dialog and some sound. The simple and most common alert window that I found does not work for me, since it is impossible to create an alert from a service. Thanks to Harsch for giving me an idea of ​​the activities. I will send the code at the moment when it will work. Thanks in advance, as promised, the working code of the warning window called from the service: 1) the code in the service file:

    Intent intent;
    intent = new Intent(this, MyAlert.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

2) the code in the .java activity file:

    public class MyAlert extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_alert);

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    // set title
    alertDialogBuilder.setTitle("Your Title");

    // set dialog message
    alertDialogBuilder
            .setMessage("Your Message")
            .setCancelable(false)
            .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    stopService(getIntent());
                    dialog.cancel();
                    finish();
                }
            });

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();
}

3) , 4) manifest.xml:

    <activity
        android:name="com.igor.map.MyAlert"
        android:theme="@android:style/Theme.Dialog">
    </activity>

, , Ok, , , .

+4
1

.

<activity
        android:name="com.example.DialogActivity"
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.Dialog">
+8

All Articles