我正在設計一個應用程序,並且需要使用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);
}
}