創建服務,並從MainActivity.class
與放串額外給意圖調用,但是當我在onStartCommand稱之爲getStringExtra
,它返回NullPointerException異常獲取意圖的服務回報NullPointerException異常
這裏是我的代碼:
MainService.class:
public class MainService extends Service {
private WindowManager wm;
private WindowManager.LayoutParams params;
@Override
public IBinder onBind(Intent i) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
if (mView != null) {
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(mView);
}
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final String quote = intent.getStringExtra("quote");
Log.d("datastring",quote);
return super.onStartCommand(intent, flags, startId);
}
}
在MainActivity.class我叫:
Intent i=new Intent(MainActivity.this, MainService.class);
i.putExtra("quote", "dataquote");
stopService(i);
如何從MainActivity獲取字符串在MainService中?
我們無法從'stopService'獲取'intent'。這也被問過昨天,在這裏檢查:http://stackoverflow.com/questions/39191968/get-intent-passed-to-stopserviceintent-in-service/39192026#39192026 – Shaishav
@Shaishav我如何能得到意圖在服務? –
您是否嘗試在'MainActivity.class'中使用'startService(i)',而不是'stopService(i)'? –