2012-10-15 52 views
0

我正在使用此代碼創建一個即使應用程序未運行也必須運行的BroadcastReceiver。BroadcastReceiver在android中不工作

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicHeader; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.util.Log; 

public class ReminderClass extends BroadcastReceiver { 
    SharedPreferences myPrefs; 
    String prefuName, pref_usr_fname, prefdev_id, pref_usr_lname, pref_uid, 
      pref_usr_email, ret, dates_btw;; 
    String event_desc, e_time, s_time, event_title, day_event_id; 
    ArrayList<String> day_data = new ArrayList<String>(); 
    ArrayList<String> st_time = new ArrayList<String>(); 
    ArrayList<String> et_time = new ArrayList<String>(); 
    ArrayList<String> day_eve_id = new ArrayList<String>(); 
    ArrayList<String> day_eve_title = new ArrayList<String>(); 
    int MILLIS_IN_FIFTEEN_MINS = 1000 * 60 * 15 ; 

    SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a"); 
    @Override 
    public void onReceive(Context con, Intent _intent) { 
     // TODO Auto-generated method stub 
     Log.v("inside the broadcast receiver1 >>","broadcast1"); 
     if (_intent.getAction().equals("REMINDER")) { 

     Log.v("inside the broadcast receiver2 >>","broadcast2"); 
     myPrefs = con.getSharedPreferences("myPrefs", 1); 
     prefuName = myPrefs.getString("u_name", ""); 
     pref_usr_fname = myPrefs.getString("fname", ""); 
     prefdev_id = myPrefs.getString("dev_id", ""); 
     pref_usr_lname = myPrefs.getString("lname", ""); 
     pref_uid = myPrefs.getString("id", ""); 
     pref_usr_email = myPrefs.getString("email", ""); 

     Log.v("pref name>>> ", prefuName); 
     Log.v("pref pass >>", pref_usr_fname); 
     Log.v("prev devid >>", prefdev_id); 
     Log.v("pref_usr_fname", pref_usr_fname); 
     Log.v("pref_usr_lname", pref_usr_lname); 
     Log.v("pref_uid", pref_uid); 
     Log.v("pref_usr_email", pref_usr_email); 

     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout 
                        // Limit 

      HttpResponse response; 
      HttpPost post = null; 
      JSONObject json = new JSONObject(); 

       post = new HttpPost("my_url"); 
       json.put("user_id", pref_uid); 

       post.setHeader("Content-Type", "application/json"); 
       post.setHeader("Accept", "application/json"); 
       StringEntity se = new StringEntity(json.toString()); 
       se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, 
         "application/json")); 
       post.setEntity(se); 
       response = client.execute(post); 
       Log.v("Response >>>", response + ""); 
       ret = EntityUtils.toString(response.getEntity()); 
       Log.v("My calendar response", ret); 
     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (ClientProtocolException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (org.apache.http.ParseException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (JSONException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     }   
    } 
     else 
     { 
      Log.v("Broadcast not working >>","broadcast not working"); 
     } 
    } 

} 

在我的清單,

<receiver 
      android:name=".ReminderClass" 
      android:enabled="true" android:permission="android.permission.INTERNET"> 
      <intent-filter> 
       <action android:name="REMINDER" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </receiver> 

我的廣播沒有被調用。我做錯了什麼?

回答

0

我解決了我的問題。我已經添加了一個服務,並在BroadcastReceiver中調用該服務,該服務檢查啓動完成時間。在服務的Oncreate()方法中,我實現了一個每分鐘運行的線程。我在線程的run方法內使用我的代碼。

0

步驟#1:從<intent-filter>中刪除android:permission,除非您確實要求發送廣播的任何人必須擁有INTERNET權限。

步驟#2:使用一個唯一的操作字符串,可能是一個包含您的包名稱的字符串,而不是REMINDER

第3步:將您的所有代碼移出BroadcastReceiver並轉入其他內容,如IntentService。您當前的代碼正在主應用程序線程上執行網絡I/O。因此,您的代碼將在Android 4.x設備上崩潰,並且在早期的Android版本中僅僅是一個不好的想法。

+0

我遵循了這些步驟。將REMINDER更改爲MYINTENT。刪除了BroadcastReceiver中的代碼。 Intent i = new Intent(con,my_package.TestActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); con.startActivity(i);但我的BroadcastReceiver沒有被調用。我正在使用另一個廣播來檢查devicebootup。設備啓動完成後,我啓動了我的應用程序。在一個應用程序中使用兩個BroadcastReceiver是錯誤的。 – Manikandan

相關問題