2014-04-15 143 views
0

我正在设计一个应用程序,并且需要使用IntentService来显示手机测量的分贝级别。为此,我将使用IntentService。目前我实施了这项服务,但似乎无法使其发挥作用。我采取了以下措施:Android IntentService永远不会被调用

  • 在我的MainActivity检索邮件发送创建一个BroadcastReceiver我的IntentService(DbResponse)(107线)

  • 注册服务在我在MainActivity的onCreate方法( Line 33-36)

  • 创建名为SoundMeasurmentService的IntentService并实现onHandleIntent函数。 (SoundMeasurementSercie)从onHandleIntent内部

  • 广播消息与由所述的IntentFilter

使用的相同的设置我把一些system.outs的SoundMeasurementService内并且这些从来没有打印。让我觉得这项服务从未被称为。但startSerice与服务一起调用,所以应该调用imo。

MainActiviyt.java

public class MainActivity extends Activity { 

public final static String EXTRA_MESSAGE = "com.example.noisetubeinteractive2.EXTRA_MESSAGE"; 
private DbResponse dbResponse; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Registering service with category and filter (defined in SoundMeasuremntService) 
    IntentFilter filter = new IntentFilter(DbResponse.ACTION_RESP); 
    filter.addCategory(Intent.CATEGORY_DEFAULT); 
    dbResponse = new DbResponse(); 
    registerReceiver(dbResponse, filter); 

    //Adding fragmets to the mainActivity 
    if (savedInstanceState == null) { 
     getFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()).commit(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 

    switch (item.getItemId()) { 
     case R.id.action_measurement: 

      System.out.println("actoin_measurement clicked"); 
      return true; 
     case R.id.action_profile: 
      startProfile(); 
      System.out.println("action_profile clicked"); 
      return true; 
     case R.id.action_settings: 

      System.out.println("action_settngs clicked"); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 

} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 

    TextView textDbLvl; 
    Intent mServiceIntent; 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container,false); 

     textDbLvl = (TextView) rootView.findViewById(R.id.db_lvl); //Getting field that needs to be edited with response form broadcaster 
     System.out.println("onCreateView of the PlaceholderFragment"); //This is printed 

     mServiceIntent = new Intent(getActivity(), SoundMeasurementService.class); //Creating Intent to pass to Service 
     mServiceIntent.putExtra(SoundMeasurementService.PARAM_IN_MSG, "This it the IN_MSG"); //Adding some dataString to the Intent to pass to service 
     getActivity().startService(mServiceIntent); //Starting service with the intent 

     System.out.println("Service is started"); 
     return rootView; 
    } 


} 

public class DbResponse extends BroadcastReceiver { 

    TextView textDbLvl; 
    private String dbLvlString = "0"; 
    private static final String dbStringAppend = " db"; 
    public static final String ACTION_RESP = "com.noisetube.main.intent.action.MESSAGE_PROCESSED"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     dbLvlString = intent.getStringExtra(SoundMeasurementService.PARAM_OUT_MSG); //Retrieving message from the intend by the Serice 
     System.out.println("Received from broadcast: " + dbLvlString); 
     textDbLvl.setText(dbLvlString + dbStringAppend); //Set layout to received message 
    } 
} 

public void sendMessage(View view) { 
    //Resoponding on teh button click 'Send' 
    Intent intent = new Intent(this, DisplayMessageActivity.class); //Intent used to start another activity (binding between seperate components) 
                    //2nd par: class of component witch to deliver the intent 
    EditText editText = (EditText) findViewById(R.id.edit_message); 
    String message = editText.getText().toString(); 
    intent.putExtra(EXTRA_MESSAGE, message); //Values linked to data, EXTRA_MESSAGE should be added as public final static 

    startActivity(intent); 
} 

public void startProfile() { 
    Intent intent = new Intent(this, NewProfileActivity.class); 
    startActivity(intent); 
} 

}

SoundMeasurmentService.java

public class SoundMeasurementService extends IntentService { 

private int dbLvl= 10; 
public static final String DB_LVL = "DB_LVL"; 
public static final String PARAM_IN_MSG = "inMsg"; 
public static final String PARAM_OUT_MSG = "outMsg"; 
//TODO create soundmeter class 

public SoundMeasurementService() { 
    super("SoundMeasurementService"); 
    System.out.println("SoundMeasurmentService Created"); 
} 

@Override 
protected void onHandleIntent(Intent workIntent) { 

    System.out.println("Called onHandleIntent"); 
    String msg = workIntent.getStringExtra(PARAM_IN_MSG); 
    Intent broadcatIntent = new Intent(); 
    broadcatIntent.setAction(DbResponse.ACTION_RESP); 
    broadcatIntent.addCategory(Intent.CATEGORY_DEFAULT); 
    broadcatIntent.putExtra(PARAM_OUT_MSG, msg + "and msg out"); 
    sendBroadcast(broadcatIntent); 
} 

}

回答

0

我有可能通过改变Androidmanifest服务声明来修复这个错误。 xml文件。

旧代码:

<service android:name=".SoundMeasurementService" android:exported="false"/> 

新代码:

<service android:name="com.noisetube.main.SoundMeasurementService" android:exported="false"/> 
相关问题