在服务位置监听器发送通知的问题 [英] Location listener in a service sends notification issue

查看:196
本文介绍了在服务位置监听器发送通知的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是从每次的位置发生变化的服务中发送通知。当用户点击了通知我想通知关闭,并开始活动。我设法发送通知,但是当该通知被窃听它并不清楚,也没有启动该活动。我看不到我要去的地方不对的!?这里是code:

What i am trying to do is send a notification from within a service whenever the location is changed. When the user taps on the notification i want the notification to close and start an activity. I managed to send the notification but when the notification is tapped it doesn't clear nor it starts the activity. I can't see where i am going wrong with this!? Here is the code:

package com.oxinos.android.moc;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Toast;


public class mocService extends Service implements OnClickListener{

    NotificationManager nm;
    static final int uniqueID1= 190910;
    PendingIntent pi;
    Context con;

    @Override
    public IBinder onBind(Intent intent) {

        return null;

    }

    @Override
    public void onCreate() {
        super.onCreate();
        con = getApplicationContext();

        Intent intent = new Intent(this, mocActivity2.class);

        pi = PendingIntent.getService(this, 0, intent, 0);
        nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                String locStr = "New loc: "+location.getLatitude()+","+location.getLongitude();
                String title = "New MOC notification";

                Notification startupNotification = new Notification(R.drawable.ic_launcher, locStr, System.currentTimeMillis());
                startupNotification.setLatestEventInfo(con, title,locStr, pi);
                startupNotification.defaults = Notification.DEFAULT_ALL;
                nm.notify(uniqueID1, startupNotification);
                //nm.cancel(uniqueID1);

            }

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}

            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
                // TODO Auto-generated method stub

            }
        };

        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100000, 50, locationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100000 , 50, locationListener);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStart(Intent intent, int startId) {

        super.onStart(intent, startId);

        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();

    }

    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        startActivity(new Intent(this, mocActivity2.class));
        nm.cancel(uniqueID1);
    }
}

任何想法?我究竟做错了什么?

Any ideas??? What am i doing wrong?

推荐答案

您正在创建你的的PendingIntent 使用的getService()。如果你想开始与的PendingIntent ,你需要使用 getActivity()

You are creating your PendingIntent using getService(). If you want to start an activity with the PendingIntent, you need to use getActivity().

这篇关于在服务位置监听器发送通知的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆