[英]Stop GcmListenerService progmatically
I am using GCM. 我正在使用GCM。 I want to stop GcmListenerService so I can stop Application from receiving unwanted notification. 我想停止GcmListenerService,以便可以阻止应用程序收到不需要的通知。
I have tried the below to close the service but it doesn't seems to workout 我已经尝试过以下方法关闭服务,但似乎无法锻炼
Intent intent = new Intent(this, MyGcmListenerService.class);
stopService(intent);
Here is my service class 这是我的服务班
public class MyGcmListenerService extends GcmListenerService
{
private static final String TAG = "MyGcmListenerService";
// [START receive_message]
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
String duration = data.getString("duration");
Log.d(TAG, "From: " + from);
Log.d(TAG, "Message: " + message);
sendNotification(message,duration);
}
// [END receive_message]
private void sendNotification(String message, String duration) {
new DisplayNotification(this).makeGCMNotification("Event Name", "Event Start Time", 1);
}
}
and in manifest 并在清单中
<service
android:name=".Utilities.GCM.MyGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</service>
Stopping the service won't help because the GCMReceiver will always call startWakefulService
whenever a new message comes in. Since the GCMReceiver class is not really something we're supposed to edit, I suggest that you keep a boolean or set a preference to know when received messages should actually be read/processed. 停止服务无济于事,因为每当startWakefulService
时,GCMReceiver都会始终调用startWakefulService
。由于GCMReceiver类并不是我们真正应该编辑的东西,因此建议您保留一个布尔值或设置一个首选项以了解何时收到的消息实际上应该被读取/处理。 Based on this, you can let the messages get received in the background while actually doing nothing in the app/foreground. 基于此,您可以让消息在后台收到,而实际上在应用程序/前景中什么也不做。
You can unsubscribe from GCM with two methods: deleteToken()
and deleteInstanceID()
. 您可以使用两种方法退订GCM: deleteToken()
和deleteInstanceID()
。 Both approaches generate IOExceptions so they must be handled as well. 两种方法都会生成IOException,因此也必须对其进行处理。
Please look at the "Unregistration and Unsubscription" section . 请查看“取消注册和取消订阅”部分 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.