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

标签 java android service alarm

我之前问过如何在服务中显示 AlertDialog,我在搜索时找到了这个答案:Here

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

任何人都可以帮忙解决这些问题花了很多时间吗:"(

这是我的代码:

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>

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

最佳答案

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

}
}

关于java - 通过调用 Activity 在服务中显示 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9672256/

相关文章:

windows - 在用户文件夹中安装 PostgreSQL 数据

windows - 即使使用同一用户进行服务,也无法通过 Windows 服务访问 UNC 路径

使用简单的开关读取和写入 xml 或 json 的 Java 库

java - 设置 JSeperator 的宽度

android - Glide库支持exif旋转

Android:为什么我在完全限定的类名和 "WARN/ActivityManager(52): Unable to start service Intent"中有一个斜杠

java - 我可以对带分隔符的文件使用 Eclipselink Moxy 编码吗?

java - 消息聊天不太好用

android - 异常 : OutOfMemoryError

java - Android - 如何在 GridView 中选择一个项目?