Broadcastreciever不会启动活动

时间:2016-03-08 10:33:30

标签: android android-intent android-activity android-service android-broadcastreceiver

我的应用程序基本检测到屏幕解锁并弹出一个祝酒词。它的工作正常,但是当我实现一个Intent并调用另一个活动时它会崩溃..

NodeParser.prototype.getChildren = function(parentContainer) {
    return flatten([].filter.call(parentContainer.node.childNodes, renderableNode).map(function(node) {
        var container = [node.nodeType === Node.TEXT_NODE && node.parentElement.tagName !== "text" ? new TextContainer(node, parentContainer) : new NodeContainer(node, parentContainer)].filter(nonIgnoredElement);
        return node.nodeType === Node.ELEMENT_NODE && container.length && node.tagName !== "TEXTAREA" ? (container[0].isElementVisible() ? container.concat(this.getChildren(container[0])) : []) : container;
    }, this));
};

我在这里添加了开始活动的意图。

package com.androidexample.screenonoff;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class AEScreenOnOffService extends Service  {
    BroadcastReceiver mReceiver=null;

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

            // Toast.makeText(getBaseContext(), "Service on create", Toast.LENGTH_SHORT).show();

            // Register receiver that handles screen on and screen off logic
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            mReceiver = new AEScreenOnOffReceiver();
            registerReceiver(mReceiver, filter);

        }

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

            boolean screenOn = false;

            try{
                // Get ON/OFF values sent from receiver ( AEScreenOnOffReceiver.java ) 
                screenOn = intent.getBooleanExtra("screen_state", false);

            }catch(Exception e){}

             //  Toast.makeText(getBaseContext(), "Service on start :"+screenOn, 
                    //Toast.LENGTH_SHORT).show();

            if (!screenOn) {

                // your code here
                // Some time required to start any service
                Toast.makeText(getBaseContext(), "Begin ", Toast.LENGTH_LONG).show();

我已经检查了xml的新活动,这个类似乎都很好,但似乎没有任何工作......

1 个答案:

答案 0 :(得分:0)

试试这种方式

  Intent yourintent = new Intent(this, MyActivity.class);
            yourintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(yourintent);

希望这有助于你