不显示检测到的活动的通知 [英] Doesnot show notification on detected activity

查看:71
本文介绍了不显示检测到的活动的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些检测活动的代码。但是通知没有显示我选择哪个行走;以下 清单:





< uses-permission android:name =   com.google.android.gms.permission.ACTIVITY_RECOGNITION>< /使用的许可> 

< application
android:allowBackup = true
android:icon = @ drawable / ic_launcher
android:label = @ string / app_name
android:theme = @ style / AppTheme>
< activity
android:name = 。MainActivity
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 = 。ActivityRecognizedService>< / service>
< / application>



我创建了Intentserivice,通过待定意图在3秒后调用;



以下是主要活动:



  import  android。 app.PendingIntent; 
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.ActivityRecognition;


public class MainActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {

public GoogleApiClient mApiClient;


@覆盖
受保护 void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mApiClient = new GoogleApiClient.Builder(
。 addApi(ActivityRecognition.API)
.addConnectionCallbacks( this
.addOnConnectionFailedListener( this
.build();

mApiClient.connect();


}



@ Override
< span class =code-keyword> public void onConnected( @ Nullable 捆绑包) {
Intent intent = new Intent( this ,ActivityRecognizedService。);
PendingIntent pendingIntent = PendingIntent.getService( this 0
intent,PendingIntent .FLAG_UPDATE_CURRENT);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
mApiClient, 3000 ,pendingIntent);
}

@覆盖
public void onConnectionSuspended( int i){

}

@覆盖
public void onConnectionFailed( @ NonNull ConnectionResult
connectionResult){

}

}

是IntentSerice

public class ActivityRecognizedService extends IntentService {

public ActivityRecognizedService(){
super ActivityRecognizedService);
}

public ActivityRecognizedService( String name){
super (name);
}

@覆盖
受保护 void onHandleIntent(Intent intent){
if (ActivityRecognitionResult.hasResult(intent)){
ActivityRecognitionResult result =
ActivityRecognitionResult.extractResult(intent);
handleDetectedActivities(result.getProbableActivities());
}
}

私有 void handleDetectedActivities(列表< DetectedActivity>
probableActivities){
for (DetectedActivity activity:probableActivities){
switch (activity.getType()){
case DetectedActivity.IN_VEHICLE:{
Log.e( ActivityRecogition 在车辆中: +
activity.getConfidence());
break ;
}
case DetectedActivity.ON_BICYCLE:{
Log.e( ActivityRecogition On Bicycle: +
activity.getConfidence());
break ;
}
case DetectedActivity.ON_FOOT:{
Log.e( ActivityRecogition 徒步: +
activity.getConfidence());
break ;
}
case DetectedActivity.RUNNING:{
Log.e( ActivityRecogition 正在运行: +
activity.getConfidence());
break ;
}
case DetectedActivity.STILL:{
Log.e( ActivityRecogition 仍然: +
activity.getConfidence());
break ;
}
case DetectedActivity.TILTING:{
Log.e( ActivityRecogition 倾斜: +
activity.getConfidence());
break ;
}
case DetectedActivity.WALKING:{
Log.e( ActivityRecogition 行走: +
activity.getConfidence());
if (activity.getConfidence()> = 75 ){
NotificationCompat .Builder builder = new
NotificationCompat.Builder( this );
builder.setContentText( 你走路了吗?);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle(getString(
R.string.app_name));
NotificationManagerCompat.from( this )。notify( 0
builder.build ());
}
break ;
}
case DetectedActivity.UNKNOWN:{
Log.e( ActivityRecogition 未知: +
activity.getConfidence());
break ;
}
}
}
}

}



所以当我运行这个应用程序时它没有检测到任何活动,也没有向我显示此代码中的错误通知:



日志文件显示:

仍然= 100 
倾斜= 100
onbike = 18
在车辆上= 18等



请问哪一件事我包括错误



我尝试过的事情:



我试过但是没有显示任何动作我的意思是跑步,走路等

解决方案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

I created some code that Detect the activity. But the notification does not show which i selected for the walking; the following is the manifest:



<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        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=".ActivityRecognizedService"></service>
</application>


I created Intentserivice which call after 3 second by pending intent;

the following is the main activity:

import android.app.PendingIntent;
          import android.content.Intent;
           import android.os.Bundle;
          import android.support.annotation.NonNull;
          import android.support.annotation.Nullable;
         import android.support.v7.app.AppCompatActivity;

           import com.google.android.gms.common.ConnectionResult;
           import com.google.android.gms.common.api.GoogleApiClient;
           import com.google.android.gms.location.ActivityRecognition;


         public class MainActivity extends AppCompatActivity implements 
         GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {    

    public GoogleApiClient mApiClient;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mApiClient = new GoogleApiClient.Builder(this)
                .addApi(ActivityRecognition.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        mApiClient.connect();


    }



    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Intent intent = new Intent( this, ActivityRecognizedService.class );
        PendingIntent pendingIntent = PendingIntent.getService( this, 0,  
              intent, PendingIntent.FLAG_UPDATE_CURRENT );
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( 
                           mApiClient, 3000, pendingIntent );
       }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult  
             connectionResult) {

    }

}

and this is the IntentSerice class:

             public class ActivityRecognizedService extends IntentService {

      public ActivityRecognizedService() {
        super("ActivityRecognizedService");
        }

      public ActivityRecognizedService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if(ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult result = 
                   ActivityRecognitionResult.extractResult(intent);
            handleDetectedActivities( result.getProbableActivities() );
        }
    }

    private void handleDetectedActivities(List<DetectedActivity> 
                probableActivities) {
        for( DetectedActivity activity : probableActivities ) {
            switch( activity.getType() ) {
                case DetectedActivity.IN_VEHICLE: {
                    Log.e( "ActivityRecogition", "In Vehicle: " + 
                              activity.getConfidence() );
                    break;
                }
                case DetectedActivity.ON_BICYCLE: {
                    Log.e( "ActivityRecogition", "On Bicycle: " + 
                            activity.getConfidence() );
                    break;
                }
                case DetectedActivity.ON_FOOT: {
                    Log.e( "ActivityRecogition", "On Foot: " + 
                            activity.getConfidence() );
                    break;
                }
                case DetectedActivity.RUNNING: {
                    Log.e( "ActivityRecogition", "Running: " + 
                            activity.getConfidence() );
                    break;
                }
                case DetectedActivity.STILL: {
                    Log.e( "ActivityRecogition", "Still: " + 
                            activity.getConfidence() );
                    break;
                }
                case DetectedActivity.TILTING: {
                    Log.e( "ActivityRecogition", "Tilting: " + 
                             activity.getConfidence() );
                    break;
                }
                case DetectedActivity.WALKING: {
                    Log.e( "ActivityRecogition", "Walking: " + 
                            activity.getConfidence() );
                    if( activity.getConfidence() >= 75 ) {
                        NotificationCompat.Builder builder = new 
                        NotificationCompat.Builder(this);
                        builder.setContentText( "Are you walking?" );
                        builder.setSmallIcon( R.mipmap.ic_launcher );
                        builder.setContentTitle( getString( 
                                                 R.string.app_name ) );
                        NotificationManagerCompat.from(this).notify(0, 
                                         builder.build());
                    }
                    break;
                }
                case DetectedActivity.UNKNOWN: {
                    Log.e( "ActivityRecogition", "Unknown: " + 
                            activity.getConfidence() );
                    break;
                }
            }
        }
       }

        }


So when i run this app it doesnot detect any activity and doesnot show me the notification what is wrong in this code:

the log file show:

still=100
          tilting=100
          onbike=18
          on vehicle=18 etc


please which thing i have included wrong

What I have tried:

I tried but does not show any action i mean runing,walking etc

解决方案

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


这篇关于不显示检测到的活动的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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