进入位置时报警无效

时间:2015-10-10 07:03:19

标签: android dictionary alarmmanager

我想在用户进入特定位置时触发警报。当用户进入该位置时,我试图将单元格从静音模式切换到正常模式。但是当我在我的应用程序中添加代码时,它无效。 请告诉我,我哪里错了?

Logcat错误

10-10 11:40:39.711: E/AndroidRuntime(26894): FATAL EXCEPTION: main
10-10 11:40:39.711: E/AndroidRuntime(26894): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.haider.trymap/com.haider.trymap.ProximityActivity}: java.lang.NullPointerException
10-10 11:40:39.711: E/AndroidRuntime(26894):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at android.app.ActivityThread.access$700(ActivityThread.java:157)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at android.os.Looper.loop(Looper.java:176)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at android.app.ActivityThread.main(ActivityThread.java:5317)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at java.lang.reflect.Method.invokeNative(Native Method)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at java.lang.reflect.Method.invoke(Method.java:511)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at dalvik.system.NativeStart.main(Native Method)
10-10 11:40:39.711: E/AndroidRuntime(26894): Caused by: java.lang.NullPointerException
10-10 11:40:39.711: E/AndroidRuntime(26894):    at com.haider.trymap.ProximityActivity.onCreate(ProximityActivity.java:49)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at android.app.Activity.performCreate(Activity.java:5326)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
10-10 11:40:39.711: E/AndroidRuntime(26894):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
10-10 11:40:39.711: E/AndroidRuntime(26894):    ... 11 more

ProximityActivity.java

(给出错误的代码段)

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

boolean proximity_entering = getIntent().getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);
double lat = getIntent().getDoubleExtra("lat", 0);

double lng = getIntent().getDoubleExtra("lng", 0);

String strLocation = Double.toString(lat)+","+Double.toString(lng);


        if(proximity_entering){
            Toast.makeText(getBaseContext(),"Entering the region"  ,Toast.LENGTH_LONG).show();
            notificationTitle="Proximity - Entry";
            notificationContent="Entered the region:" + strLocation;
            tickerMessage = "Entered the region:" + strLocation;

            Intent normal = new Intent(getBaseContext(), Normal_Mode.class);
            PendingIntent nor = PendingIntent.getBroadcast(getBaseContext(), RQS_1, normal, 0);
            AlarmManager noralarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            noralarm.set(AlarmManager.RTC_WAKEUP, targetcal.getTimeInMillis(), nor);


        }else{
            Toast.makeText(getBaseContext(),"Exiting the region"  ,Toast.LENGTH_LONG).show();
            notificationTitle="Proximity - Exit";
            notificationContent="Exited the region:" + strLocation;
            tickerMessage = "Exited the region:" + strLocation;

        }

        Intent notificationIntent = new Intent(getApplicationContext(),NotificationView.class);
        notificationIntent.putExtra("content", notificationContent );

        /** This is needed to make this intent different from its previous intents */
        notificationIntent.setData(Uri.parse("tel:/"+ (int)System.currentTimeMillis()));

        /** Creating different tasks for each notification. See the flag Intent.FLAG_ACTIVITY_NEW_TASK */
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

        /** Getting the System service NotificationManager */
        NotificationManager nManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

        /** Configuring notification builder to create a notification */
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                            .setWhen(System.currentTimeMillis())
                            .setContentText(notificationContent)
                            .setContentTitle(notificationTitle)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setAutoCancel(true)
                            .setTicker(tickerMessage)
                            .setContentIntent(pendingIntent)
                            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));                            


        /** Creating a notification from the notification builder */
        Notification notification = notificationBuilder.build();

        /** Sending the notification to system. 
         * The first argument ensures that each notification is having a unique id 
         * If two notifications share same notification id, then the last notification replaces the first notification 
         * */
        nManager.notify((int)System.currentTimeMillis(), notification);

        /** Finishes the execution of this activity */
        finish();


    }
    }

0 个答案:

没有答案