Отображение AlertDialog в сервисе путем вызова действия - PullRequest
0 голосов
/ 12 марта 2012

Я спрашивал ранее, как я могу показать AlertDialog в сервисе, и я искал, я нашел этот ответ: Здесь

, но я не знаю, как я могу вызвать Activity из сервиса??Я написал этот код, и я не знаю, почему возникает ошибка: приложение остановилось неожиданно!

Может кто-нибудь помочь pleassssssssse, эта проблема заняла много времени: "(

это мой код:

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() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG)
                .show();
        return null;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
                .show();
    }

    @Override
public void onStart(Intent intent, int startId) {
 // TODO Auto-generated method stub
 super.onStart(intent, startId);
 Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();

Intent i = new Intent (this, ShowingDialogActivity.class);
startActivity(i);



}

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG)
                .show();
        return super.onUnbind(intent);
    }

}

// This is ShowingDialogActivity

import android.app.Activity; 
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class ShowingDialog extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        //For Notification
         final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            //For Notification
            alertDialog.setTitle("Conformation");
            alertDialog.setMessage("Are you sure you want to Expand Report Region?");


                    alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {
                          // here you can add functions
                          // Sending a Message to server that the plaintiff wants to expand report region

                       }
                    });

                    alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {
                        // here you can add functions
                        // Do nothing 


                       }
                    });

                    alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                    alertDialog.show();

    }




}

// the Manifest

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroidAlarmService"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".ShowingDialog"
                  android:theme="@android:style/Theme.Translucent"
                  ></activity>



        <service android:name=".MyAlarmService"

             />
    </application>
</manifest>

Может кто-нибудь сказать мне, что не так с этим кодом ??????????

Ответы [ 2 ]

1 голос
/ 12 марта 2012

Обновите свой сервис: для запуска деятельности из фона вы должны установить флаг Intent.FLAG_FROM_BACKGROUND

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() {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG)
                .show();

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG)
                .show();
        return null;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG)
                .show();
    }

    @Override
public void onStart(Intent intent, int startId) {
 // TODO Auto-generated method stub
 super.onStart(intent, startId);
 Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();

 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(
                                Intent.CATEGORY_LAUNCHER).setClassName("YOUR_PACKAGE_NAME",
                                "YOUR_PACKAGE_NAME.ShowingDialog.class").addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                .addFlags(Intent.FLAG_FROM_BACKGROUND).setComponent(new ComponentName("YOUR_PACKAGE_NAME",
                            "YOUR_PACKAGE_NAME.ShowingDialog.class"));
                 getApplicationContext().startActivity(intent);



}

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG)
                .show();
        return super.onUnbind(intent);
    }

}
}
0 голосов
/ 12 марта 2012

Имеется ли в вашем приложении действие AndroidAlarmService , как указано в манифесте Android?

 <activity android:name=".AndroidAlarmService"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
...