从接收器启动android活动(在运行服务内)

时间:2012-12-01 02:46:37

标签: android android-intent

我希望在收到短信时显示提醒,所以我的想法是启动一个启动alertdialog的新活动。

我的服务开始没有问题,并启动接收器..

我可以收到短信并显示吐司警报。但我想改为显示自定义警报。

这是启动我的服务的活动(ServiceExampleActivity):

public class ServiceExampleActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btnStartService = (Button) findViewById(R.id.btnStart);
        Button btnStopService = (Button) findViewById(R.id.btnStop);

        btnStartService.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                StartMyService();
            }
        });

        btnStopService.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                StopMyService();
            }
        });          

    }

    private void StartMyService() {
        Intent myServiceIntent = new Intent(this, ServiceTest.class);
        startService(myServiceIntent);
    }

    private void StopMyService() {
        Intent myServiceIntent = new Intent(this, ServiceTest.class);
        stopService(myServiceIntent);
    }        
}

这是我的服务(ServiceTest):

public class ServiceTest extends Service {

    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
    private BroadcastReceiver myBroadcastReceiver = null;

    @Override
    public void onCreate() {
        super.onCreate();

        final IntentFilter theFilter = new IntentFilter();
        theFilter.addAction(ACTION);

        this.myBroadcastReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                StartDialogActivity(context, intent);

            }
        };

        this.registerReceiver(myBroadcastReceiver, theFilter);
    }

    @Override
    public IBinder onBind(Intent arg) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        Log.d("ServiceTest", "Started");
        Toast.makeText(this, "Service started...", 3000).show();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        unregisterReceiver(myBroadcastReceiver);
        Toast.makeText(this, "Service destroyed...", 3000).show();
    }

    private void StartDialogActivity(Context context, Intent intent) {
        Intent dlgIntent = new Intent(context, DialogActivity.class);               
        dlgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      
        context.startActivity(dlgIntent);   
    }
}

这是我想要启动的活动,通常会显示alertdialog ..

public class DialogActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);       

    }
}

当它尝试启动活动时,应用程序崩溃了.. 你能告诉我这是错误吗?? ??

1 个答案:

答案 0 :(得分:3)

像这样,你想要从服务开始活动

Intent dlgIntent = new Intent(context, DialogActivity.class);
dlgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dlgIntent );