关于BroadcastReceiver启动Service的问题。
我需要利用一个开机启动的BroadcastReceiver启动一个Service , 然后再service中,利用WindowManager将自己定义的一个button加在正在运行的view上面。
但是我不知道哪里错了,请大家帮下忙,看下。
MyReceiver.java 开机即启动这个receiver,并在receiver中启动下面定义的service
package com.sav;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, SAV.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(i);
Log.v("receiver" , "on receive");
}
}
package com.sav;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.view.WindowManager;
import android.widget.Button;
public class MyServer extends Service {
public final static String FIELD_EVENT = "event";
private WindowManager mWm;
private Button buttonview;
WindowManager.LayoutParams mParams;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
mWm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
buttonview = new Button(this);
buttonview.setText("window manager test!");
mParams = new WindowManager.LayoutParams();
mParams.height = 50;
mParams.width = 50;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
mWm.addView(buttonview, mParams);
}
}
package com.sav;
import android.app.Activity;
import android.os.Bundle;
public class SAV extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sav" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SAV" 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=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyServer"></service>
</application>
</manifest>
context.startService(i);//启动应该用startActivity(i)
Log.v("receiver" , "on receive");
[解决办法]
receiver怎么可以启动的,楼主说一下啊?