通过调用 Activity 在服务中显示AlertDialog

2020年6月18日 55点热度 0条评论

我问过如何在服务中显示AlertDialog,然后我在搜索时发现了以下答案:Here

但我不知道如何从服务中调用“活动”?
我写了这段代码,但我不知道为什么会出现错误:应用程序意外停止!

任何人都可以帮助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>

谁能告诉我这段代码有什么问题吗?

解决方案如下:

更新您的服务:
要从后台启动活动,必须设置标志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);
    }

}
}