2013-05-31 64 views
0

我想在應用程序安裝到設備上後立即啓動的服務中註冊兩個廣播接收器。如果設備重新啓動(在引導完成的接收器上使用),此服務也會啓動。廣播接收器不能使用服務

該服務啓動一個活動,其中觸發所有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(); 

      } 

     } 
    }; 



    } 
+0

很明顯,如果你殺了應用程序,你停止了服務本身。因此,您的接收器將不會收到任何內容,因爲該服務不再存在。 –

+0

當應用程序安裝它第一次啓動時,我開始我的服務,讓接收器註冊。現在,即使應用程序停止,服務如何被破壞,接收器也如何? –

回答

0

你是什麼意思「殺應用程序」?

從安裝應用程序時的3.1開始,它們處於「停止」狀態,因此在用戶明確啓動它們之前它們將無法運行。按下強制停止會將它們返回到此狀態。因此,首次安裝應用程序時,系統完全忽略它們,除非用戶手動啓動某些內容:最有可能的是單擊啓動程序活動或添加應用程序窗口小部件。

所以是正常的,如果你殺了應用程序。接收者都不會迴應。

0

使用了Android開始在自己的進程的服務:在清單過程屬性可能解決這個問題