广播接收器不在服务中工作

时间:2013-05-31 08:37:13

标签: android service broadcastreceiver

我正在尝试在服务中注册两个广播接收器,该服务在应用程序安装到设备上后立即启动。如果设备重新启动(使用启动完成的接收器),此服务也会启动。

该服务启动一项活动,其中触发所有UI更改和通知。广播接收器在服务内部注册工作正常,直到应用程序处于前景或后台状态。但如果我杀了应用程序。接收者似乎都没有回应。

这是我服务的代码

public class StartupService extends Service {


    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

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


        Log.i("Easy Charger Service","Service Started");
        this.registerReceiver(ChargerPluggedInReceiver, new IntentFilter(Intent.ACTION_POWER_CONNECTED));
        this.registerReceiver(ChargerPluggedOutReceiver, new IntentFilter(Intent.ACTION_POWER_DISCONNECTED));
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i("EASYCHARGER", "Service is Running");
        return super.onStartCommand(intent,flags,startId);
    }

    public BroadcastReceiver ChargerPluggedInReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub


            Toast.makeText(StartupService.this, "Charger Plugged", Toast.LENGTH_LONG).show();
            Intent startup_intent = new Intent(StartupService.this,Home.class);
            startup_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startup_intent);



        }
    };

    public BroadcastReceiver ChargerPluggedOutReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.i("EASYCHARGER", "ChargerPluggedOut");

            if(intent.getAction().equalsIgnoreCase(Intent.ACTION_POWER_DISCONNECTED )){             
                Log.i("EASYCHARGER", "ChargerPluggedOut - Performing Action Now");

                //unregisterReceiver(batteryLevelReceiver);
                Toast.makeText(StartupService.this, "Charger Unplugged", Toast.LENGTH_LONG).show();
            }

        }
    };



}

这是我的活动类

public class Home extends Activity {

    private SharedPreferences p;
    private TextView batterylevel;
    private Battery b = null;
    private NotificationsManager nm = null;
    private Button goto_tasks_act;
    private Button settings;
    private ProgressDialog pDialog;
    Context cont;
    private TextView easy;
    private boolean isFirstRun;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_home);
        cont = getApplicationContext();
        p = this.getSharedPreferences("FirstRun", MODE_PRIVATE);
        isFirstRun = p.getBoolean("FirstRun", true);
        if(isFirstRun)
        {
            Intent shortcutIntent = new Intent(getApplicationContext(),Home.class);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            Intent addIntent = new Intent();
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "EasyCharger");
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));

            addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
            this.sendBroadcast(addIntent);

            SharedPreferences.Editor editor = p.edit();
            editor.putBoolean("FirstRun", false);
            editor.commit();

            Toast.makeText(Home.this, "Registering Startup Service", Toast.LENGTH_LONG).show();
              Intent serviceIntent = new Intent(Home.this, StartupService.class);
              Home.this.startService(serviceIntent);
        }



        Typeface batman = Typeface.createFromAsset(getAssets(), "fonts/batmfa__.ttf");
        TextView easy = (TextView) findViewById(R.id.home_tv1);
        TextView charger = (TextView) findViewById(R.id.home_tv3);
//      easy.setTypeface(batman);
//      charger.setTypeface(batman);

        batterylevel = (TextView)findViewById(R.id.textView1);
        batterylevel.setText("Charging...Only Even Notifications");


        settings=(Button) findViewById(R.id.button2);
        settings.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new Intent(Home.this, Settings.class));
            }
        });

        RegisterAllReceivers(Home.this);

        goto_tasks_act = (Button)findViewById(R.id.button1);
        goto_tasks_act.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                //startActivity(new Intent(Home.this,Tasks.class));
                new ClearTasks().execute();
            }
        });

        NotificationsManager nm = new NotificationsManager(getApplicationContext());
        nm.AppActivatedNotification();
    }


    public void RegisterAllReceivers(Context context){
        Log.i("EASYCHARGER","Registering All Receivers");
        context.registerReceiver(ChargerPluggedInReceiver, new IntentFilter(Intent.ACTION_POWER_CONNECTED));
        context.registerReceiver(batteryLevelReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));        
        context.registerReceiver(ChargerPluggedOutReceiver, new IntentFilter(Intent.ACTION_POWER_DISCONNECTED));
    }


    public BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

                       ...

            }
    };






    public BroadcastReceiver ChargerPluggedInReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.i("EASYCHARGER", "ChargerPluggedIn");
            if(intent.getAction().equalsIgnoreCase(Intent.ACTION_POWER_CONNECTED )){                
                Log.i("EASYCHARGER", "ChargerPluggedIn - Performing Action Now");



            }

        }
    };

    public BroadcastReceiver ChargerPluggedOutReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.i("EASYCHARGER", "ChargerPluggedOut");

            if(intent.getAction().equalsIgnoreCase(Intent.ACTION_POWER_DISCONNECTED )){             
                Log.i("EASYCHARGER", "ChargerPluggedOut - Performing Action Now");

                //unregisterReceiver(batteryLevelReceiver);
                finish();

            }

        }
    };



    }

2 个答案:

答案 0 :(得分:0)

你是什么意思'杀死应用程序'?

  

从3.1开始安装应用程序时,它们处于“已停止”状态,因此在用户显式启动它们之前它们将无法运行。按下强制停止将使它们返回此状态。   因此,当首次安装应用程序时,系统会完全忽略它们,除非用户手动启动某些内容:点击启动器活动或添加应用程序小部件,很可能是这样。

如果你杀了应用程序,这是正常的。接收方都不会回复。

答案 1 :(得分:0)

使用manifest中的android:process属性在自己的进程中启动服务可能会解决此问题