我知道这里有很多这样的问题,但是在寻找可能的解决方案之后我找不到一个所以任何帮助在这里表示赞赏。
我有3个类文件,我的MainActivity有一个复选框,当按下时,启动一个有广播接收器的活动,我的广播接收器应该启动服务来检查用户位置。
代码非常大,所以我会尝试只显示主要部分,如果需要其他东西请告诉我。
在MainActivity中:
Public void onCheckboxClicked (View view) {
boolean checked = ((CheckBox) view).isChecked();
Switch (view. getId ()) {
case R.id.checkbox_gps:
if (checked){
Intent intent = new Intent(this, GPS_Info.class);
startActivity(intent);
}
在GPS_Info活动中:
public class GPS_Info extends Activity{
private TextView latitude;
private TextView longitude;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps_info);
}
private BroadcastReceiver GpsReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
System.out.println("Broadcast receiver");
intent.setAction("com.example.systeminfo.GpsTracker");
context.startService(intent);
}
};
我的问题是,我可以(如果是,那么如何)启动广播接收器中另一个类文件中的GpsTracker服务?我需要发送什么样的意图?我还没有完全理解意图和背景。
这是我的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.systeminfo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.systeminfo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.systeminfo.GpsReceiver" >
<intent-filter>
<action android:name="com.example.systeminfo.LOCATION_READY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="com.example.systeminfo.GPS_Info"
android:label="@string/title_activity_gps__info"
android:parentActivityName="com.example.systeminfo.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.systeminfo.MainActivity" />
</activity>
<activity
android:name="com.example.systeminfo.BatteryStatus"
android:label="@string/title_activity_battery_status"
android:parentActivityName="com.example.systeminfo.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.systeminfo.MainActivity" />
</activity>
<activity
android:name="com.example.systeminfo.General_Info"
android:label="@string/title_activity_general__info"
android:launchMode="singleInstance"
android:parentActivityName="com.example.systeminfo.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.systeminfo.MainActivity" />
</activity>
<service
android:name=".GpsTracker"
android:enabled="false"
android:exported="false" >
</service>
</application>
</manifest>
我也知道,我所描述的这个场所并不完全需要广播接收器,但这是该项目的要求。
答案 0 :(得分:0)
现在根据您的代码,您将在广播时启动该服务 com.example.systeminfo.LOCATION_READY intent ...,当这个意图将被广播时,onRecieve方法服务将启动
答案 1 :(得分:0)
是的,这是可能的)
因此,您必须创建ServiceConnection对象(mConnection)和方法doBindService(),doUnbindService()。
例如,
/**
* Class for interacting with the main interface of the service.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
mCallbackText.setText("Attached...");
// We want to monitor the service for as long as we are
// connected to it.
try {
} catch (RemoteException e) { }
}
public void onServiceDisconnected(ComponentName className) {
mCallbackText.setText("Disconnected...");
}
};
// connect to service
void doBindService() {
Intent intent = new Intent(this, MyServer.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT);
mCallbackText.setText("Binding...");
}
void doUnbindService() {
// Detach our existing connection.
unbindService(mConnection);
mCallbackText.setText("No Service...");
}
对于服务启动,您必须调用doBindService()来关闭 - doUnbindService()。