2013-04-17 19 views
2

我正在爲我正在參加的課程開展一個小組項目,我們似乎無法弄清楚爲什麼我們的報警管理器不會反覆激發意圖。我們從上到下挑選了源代碼,似乎無法隔離此問題的根源。報警管理器不會反覆執行

任何人都可以推薦一個修補程序嗎? (?或者一個起點)

//使用AlarmManager

啓動服務
Calendar cal = Calendar.getInstance(); 
    cal.add(Calendar.SECOND, 10); 
    Intent intent = new Intent(Rules.this, LMW.class); 
    PendingIntent pintent = PendingIntent.getService(Rules.this, 0, intent, 
      0); 
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
      7 * 1000, pintent); 


    // Start 2nd service using AlarmManager 
    Intent intent2 = new Intent(Rules.this, KillTimer.class); 
    PendingIntent pintent2 = PendingIntent.getActivity(Rules.this, 0, intent2, 
      0); 
    AlarmManager alarm2 = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
      120 * 1000, pintent2); // here 

來源:

public class AlarmManager extends ListActivity { 


    TextView empty; 
    TextView empty2; 
    private static final int ACTIVITY_CREATE = 0; 
    private static final int ACTIVITY_EDIT = 1; 

    public static final int INSERT_ID = Menu.FIRST; 
    private static final int DELETE_ID = Menu.FIRST + 1; 

    private List<ParseObject> todos; 
    private Dialog progressDialog; 

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> { 
     // Override this method to do custom remote calls 
     public void setVisibility() { 
       empty.setVisibility(View.VISIBLE); 
       empty2.setVisibility(View.VISIBLE); 

     } 
     protected void doInBackground(Void... params) { 
      // Gets the current list of todos in sorted order 
      ParseQuery query = new ParseQuery("TestObject"); 
      query.orderByDescending("_created_at"); 
      try { 
       todos = query.find(); 
      } catch (ParseException e) { 
       return; 
      } 

      runOnUiThread(new Runnable() { 
        public void run() { 
        }}); 
     } 

     @Override 
     protected void onPreExecute() { 
      ToDoListActivity.this.progressDialog = ProgressDialog.show(ToDoListActivity.this, "", 
        "Loading...", true); 
      super.onPreExecute(); 
     } 

     @Override 
     protected void onProgressUpdate(Void... values) { 
      super.onProgressUpdate(values); 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // Put the list of todos into the list view 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(ToDoListActivity.this, 
        R.layout.todo_row); 
      for (ParseObject todo : todos) { 
       adapter.add((String) todo.get("DataI")); 
       adapter.add((String) todo.get("DataO")); 
       adapter.add((String) todo.get("DataRSSI")); 
       adapter.add((String) todo.get("DataSSID")); 
       adapter.add((String) todo.get("DataTIME")); 
       adapter.add((String) todo.get("DataRESTRICTED")); 
      } 
      setListAdapter(adapter); 
      ToDoListActivity.this.progressDialog.dismiss(); 
     } 
    } 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_new); 

      empty = (TextView) findViewById(android.R.id.empty); 
      empty.setVisibility(View.INVISIBLE); 

     new RemoteDataTask().execute(); 
     registerForContextMenu(getListView()); 
    } 

    private void createTodo() { 
     Intent i = new Intent(this, CreateTodo.class); 
     startActivityForResult(i, ACTIVITY_CREATE); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     super.onActivityResult(requestCode, resultCode, intent); 
     if (intent == null) { 
      return; 
     } 
     final Bundle extras = intent.getExtras(); 

     switch (requestCode) { 
     case ACTIVITY_CREATE: 
      new RemoteDataTask() { 
       protected void doInBackground(Void... params) { 
        String DataI = extras.getString("DataI"); 
        String DataO = extras.getString("DataO"); 
        String DataRSSI = extras.getString("DataRSSI"); 
        String DataSSID = extras.getString("DataSSID"); 
        String DataTIME = extras.getString("DataTIME"); 
        String DataRESTRICTED = extras.getString("DataRESTRICTED"); 
        ParseObject todo = new ParseObject("Todo"); 
        todo.put("DataI", DataI); 
        todo.put("DataO", DataO); 
        todo.put("DataRSSI", DataRSSI); 
        todo.put("DataSSID", DataSSID); 
        todo.put("DataTIME", DataTIME); 
        todo.put("DataRESTRICTED", DataRESTRICTED); 
        try { todo.save(); } catch (ParseException e) { 
        } 

        super.doInBackground(); 
        return; 
       } 
      }.execute(); 
      break; 
     case ACTIVITY_EDIT: 
      // Edit the remote object 
      final ParseObject todo; 
      todo = todos.get(extras.getInt("position")); 
      todo.put("DataI", extras.getString("DataI")); 
      todo.put("DataO", extras.getString("DataO")); 
      todo.put("DataRSSI", extras.getString("DataRSSI")); 
      todo.put("DataSSID", extras.getString("DataSSID")); 
      todo.put("DataTIME", extras.getString("DataTIME")); 
      todo.put("DataRESTRICTED", extras.getString("DataRESTRICTED")); 

      new RemoteDataTask() { 
       protected void doInBackground(Void... params) { 
        try { 
         todo.save(); 
        } catch (ParseException e) { 
        } 
        super.doInBackground(); 
        return; 
       } 
      }.execute(); 
      break; 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     boolean result = super.onCreateOptionsMenu(menu); 
     menu.add(0, INSERT_ID, 0, R.string.menu_insert); 
     return result; 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     menu.add(0, DELETE_ID, 0, R.string.menu_delete); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case DELETE_ID: 
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 

      // Delete the remote object 
      final ParseObject todo = todos.get(info.position); 


      new RemoteDataTask() { 
       protected void doInBackground(Void... params) { 
        try { 
         todo.delete(); 
        } catch (ParseException e) { 
        } 
        super.doInBackground(); 
        return; 
       } 
      }.execute(); 
      return true; 
     } 
     return super.onContextItemSelected(item); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case INSERT_ID: 
      createTodo(); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Intent i = new Intent(this, CreateTodo.class); 

     i.putExtra("DataI", todos.get(position).getString("DataI").toString()); 
     i.putExtra("DataO", todos.get(position).getString("DataO").toString()); 
     i.putExtra("DataRSSI", todos.get(position).getString("DataRSSI").toString()); 
     i.putExtra("DataSSID", todos.get(position).getString("DataSSID").toString()); 
     i.putExtra("DataTIME", todos.get(position).getString("DataTIME").toString()); 
     i.putExtra("DataRESTRICTED", todos.get(position).getString("DataRESTRICTED").toString()); 
     i.putExtra("position", position); 


     startActivityForResult(i, ACTIVITY_EDIT); 
    } 

} 

服務類

public class LMW extends Service { 
     String Watchdog = "Watchdog"; 
     String Dirty1 = "playboy"; 
     String Dirty2 = "penthouse"; 
     String Dirty3 = "pornhub"; 
     String Dirty4 = "thepiratebay"; 
     String Dirty5 = "vimeo"; 
     String Dirty6 = "wired"; 
     String Dirty7 = "limewire"; 
     String Dirty8 = "whitehouse"; 
     String Dirty9 = "hackaday"; 
     String Dirty10 = "slashdot"; 
     Long mStartRX = TrafficStats.getTotalRxBytes(); 
     Long mStartTX = TrafficStats.getTotalTxBytes(); 

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

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(getApplicationContext(), "Watchdog Running!", Toast.LENGTH_SHORT).show(); 

    Long.toString(mStartTX); 
    Long.toString(mStartRX); 
    ParseObject testObject = new ParseObject("TestObject"); 
    testObject.put("DataO", String.valueOf(mStartTX)); 
    testObject.put("DataI", String.valueOf(mStartRX)); 

    testObject.saveInBackground();    String[] projection = new String[] { Browser.BookmarkColumns.TITLE, 
       Browser.BookmarkColumns.URL }; 
       Cursor cursor = getContentResolver().query(android.provider.Browser.BOOKMARKS_URI, 
         projection, null, null, null); 
       String urls = ""; 
       if (cursor.moveToFirst()) { 
       String url1 = null; 
       String url2 = null; 
       do { 
       String url = cursor.getString(cursor.getColumnIndex(Browser.BookmarkColumns.URL)); 
       Log.i(Watchdog, url); 
       if (url.toLowerCase().contains(Dirty1) || url.toLowerCase().contains(Dirty2) || url.toLowerCase().contains(Dirty3) || url.toLowerCase().contains(Dirty4) || url.toLowerCase().contains(Dirty5) || url.toLowerCase().contains(Dirty6) || url.toLowerCase().contains(Dirty7) || url.toLowerCase().contains(Dirty8) || url.toLowerCase().contains(Dirty9) || url.toLowerCase().contains(Dirty10)) 
       { 
        Intent intent2 = new Intent(LMW.this, Warning.class); 
        intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent2); 
        break; 
       }   } while (cursor.moveToNext()); 
    } 
     return START_STICKY; 
    } 

    @Override 
    public void onDestroy() { 

     super.onDestroy(); 
     Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show(); 

    } 

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

活動類別:

public class Timer extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.killtimer); 
     Toast.makeText(getApplicationContext(), "KillWifi Running!", Toast.LENGTH_SHORT).show(); 
     WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); 
     int networkId = wifiManager.getConnectionInfo().getNetworkId(); 
     wifiManager.removeNetwork(networkId); 
     wifiManager.saveConfiguration(); 

    }} 
+1

神聖的代碼,蝙蝠俠!從設置鬧鐘的位置開始怎麼樣? – codeMagic

+0

更新:)[對此感到抱歉!] –

回答

0

我有類似的問題,也許它適用於你(很難說,因爲你提供的代碼很難導航)。

我的問題是,將多個意圖應用到經理評估爲相等,覆蓋了該意圖的以前的應用程序。請參閱http://developer.android.com/reference/android/content/Intent.html#filterEquals(android.content.Intent)以瞭解我的意思是等同。作爲一個例子,我有一個使用URI domy://東西的時間表,每個意圖都有額外的變化(我需要每次都用不同的參數激發相同的動作)。附加功能不等同,所以我的報警被對方覆蓋。

爲了解決這個問題,我在一個單獨的文件中存儲了一個警報隊列,當一個人發射時,從頂部彈出下一個,並將其放入管理器中。也許這會幫助你。

UPDATE: 沒有例子公開可見的,但試試這個(觸發器是包含一個URI,它需要被解僱時的簡單對象,scheduleDb是擴展SQLiteOpenHelper類):

/** 
    * Pops the next trigger off the queue, adds it to the alarm manager, and 
    * stores it in the DB in case we need to cancel it later. 
    */ 
    private void scheduleNextTrigger() { 
    Log.d(TAG, "Popping next trigger off queue"); 
    Optional<Trigger> nextTrigger = scheduleDb.getNextTrigger(); 

    if (!nextTrigger.isPresent()) { 
     Log.d(TAG, "Trigger queue is empty"); 
     return; 
    } 
    Trigger trigger = nextTrigger.get(); 
    Log.d(TAG, "Scheduling new trigger " + trigger); 
    PendingIntent operation = createPendingIntent(trigger); 
    long timestamp = trigger.getTimestamp(); 

    // Ensure the next item is in the future 
    if (timestamp > clock.currentTimeMillis()) { 
     Log.v(TAG, "Found valid trigger: " + trigger); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, timestamp, operation); 
    } 
    scheduleDb.setScheduledTrigger(trigger); 
    } 

    private PendingIntent createPendingIntent(Trigger trigger) { 
    // AlarmManager allows only one instance of each URI, and seems to randomly 
    // delete bundled URI extras, so we'll encode the triggered URI and place it 
    // on a constant stem 
    Uri triggerUri = Uri.parse(TRIGGER_URI + "?" + Uri.encode(trigger.getUri())); 

    Intent triggerIntent = new Intent(Intent.ACTION_VIEW, triggerUri); 
    triggerIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
    Log.d(TAG, "Created trigger intent " + triggerIntent); 

    PendingIntent operation = PendingIntent.getService(this, 0, triggerIntent, 0); 

    return operation; 
    } 

希望這有助於

+0

您能否指出我如何實施這個示例? (我真的需要解決這個問題) –

+0

我已經把我能在答案中。 – lizard

+0

您是否可以使用此信息?如果是這樣,我建議你接受一個或其他答案。 – lizard