1

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

该服务启动一个活动,其中触发所有 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();

            }

        }
    };



    }
4

2 回答 2

0

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

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

如果您杀死该应用程序,这很正常。没有一个接收者会回应。

于 2013-05-31T08:55:07.650 回答
0

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

于 2014-05-12T02:55:16.650 回答