我正在尝试创建一个服务,我在5000ms之后重复调用一个方法。这个方法增加一个计数器,当它达到6时我想通过广播接收器调用另一个活动。 但是我收到一个错误“你是否错过了对unregisterreceiver()的调用”。有谁能告诉我究竟是什么问题?
主要活动
public class ServicesExampleActivity extends Activity implements OnClickListener
{
Button start,stop;
IntentFilter intentfilter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services_example);
intentfilter = new IntentFilter();
intentfilter.addAction("LOCATION_REACHED");
registerReceiver(intentreceiver, intentfilter);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if(v.getId()==R.id.start)
{
startService(new Intent(getApplicationContext(),ServiceClass.class));
}
else if(v.getId()==R.id.stop)
{
stopService(new Intent(getApplicationContext(),ServiceClass.class));
}
}
private BroadcastReceiver intentreceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("LOCATION_REACHED"))
{
Intent intent1 = new Intent(getApplicationContext(),LocationReached.class);
startActivity(intent1);
}
}
};
}
ServiceClass.java
public class ServiceClass extends Service
{
int counter = 0;
static final int UPDATE_INTREVAL = 5000; //in milliseconds
private Timer timer = new Timer();
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
doSomeThingRepeatedly();
return START_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
if(timer!=null)
{
timer.cancel();
}
Toast.makeText(this, "Service destroyed", Toast.LENGTH_LONG).show();
}
private void doSomeThingRepeatedly()
{
timer.scheduleAtFixedRate(new TimerTask()
{
@Override
public void run()
{
Toast.makeText(getApplicationContext(), ""+counter, Toast.LENGTH_SHORT).show();
counter++;
if(counter==6)
{
Intent broadcastintent = new Intent();
broadcastintent.setAction("LOCATION_REACHED");
getApplicationContext().sendBroadcast(broadcastintent);
}
}
}, 0, UPDATE_INTREVAL);
}
}
andridmanifest.xml
<activity
android:name="com.vallabh.servicesexample.ServicesExampleActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.vallabh.servicesexample.ServiceClass"></service>
<activity android:name="com.vallabh.servicesexample.LocationReached"></activity>
答案 0 :(得分:2)
“你是否错过了对unregisterReceiver()的调用”
表示您在Activity未运行时忘记取消注册BroadcastReceiver。所以使用Activity的onStop()
方法取消注册BroadcastReceiver:
@Override
protected void onStop()
{
if(null !=intentreceiver)
unregisterReceiver(intentreceiver);
super.onStop();
}
而不是在活动的onCreate
中注册BroadcastReceiver使用onResume
方法来注册广播,因为当你从下一个Activity返回时它会再次注册。
答案 1 :(得分:0)
您应该使用unregisterReceiver(theReceiver)覆盖Activity.onPause()并取消注册您已注册的接收者;