2012-04-30 15 views
0

私がやっていることは、場所が変更されたときにいつでもサービス内から通知を送信することです。ユーザーが通知をタップすると、通知が閉じられ、アクティビティが開始されます。私は通知を送信することができましたが、通知がタップされると、通知がクリアされず、アクティビティが開始されません。私はこれでどこが間違っているのか分かりません!コードは次のとおりです。サービス内のロケーションリスナーが通知の問題を送信します

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); 
    } 
} 

いずれかのアイデア???何が間違っているのですか?

答えて

0

PendingIntentgetService()を使用して作成しています。 PendingIntentでアクティビティを開始する場合は、getActivity()を使用する必要があります。

+0

ありがとうございます!それはそれだった... – user1333215

関連する問題