2013-07-07 3 views
0

Je veux mettre en œuvre Google Analytics v2 sdk androidComment implémenter Google Analytics v2 android sdk

j'avais Mise à jour AndroidManifest.xml Créez votre fichier analytics.xml mais je ne sais pas trop chaud pour mettre en œuvre des méthodes EasyTracker https://developers.google.com/analytics/devguides/collection/android/v2/#tracking-methods

Je ne sais pas où le mettre dans mon code? Désolé pour mon anglais ...

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.net.MailTo; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.Window; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 



    public class MainActivity extends Activity 

    { 

    private static final String MIME_TYPE_EMAIL = null; 
    final Activity activity = this; 

    @SuppressWarnings("deprecation") 
    @Override 


    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 
     setContentView(R.layout.activity_main); 
     webView = (WebView) findViewById(R.id.webview); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
     webView.getSettings().setPluginsEnabled(true); 
     webView.setWebChromeClient(new WebChromeClient()); 
     String scandinavianCharacters = null; 
     webView.loadDataWithBaseURL(null, scandinavianCharacters, "text/html", "UTF-8", null); 
     webView.getSettings().setUserAgentString("Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)"); 
     webView.setWebChromeClient(new WebChromeClient() { 


      public void onProgressChanged(WebView view, int progress) 
      { 
       activity.setTitle("Učitavanje..."); 
       activity.setProgress(progress * 100); 

       if(progress == 100) 
        activity.setTitle(R.string.app_name); 
      } 
     }); 

     webView.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
      { 
       try { 
        webView.stopLoading(); 
       } catch (Exception e) { 
       } 
       try { 
        webView.clearView(); 
       } catch (Exception e) { 
       } 
       if (webView.canGoBack()) { 
        webView.goBack(); 
       } 
       webView.loadUrl("file:///android_asset/greska/greska.html"); 
       super.onReceivedError(webView, errorCode, description, failingUrl); 
      } 

      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) 
      { 
       view.loadUrl(url); 
       return true; 
      } 
     }); 

     webView.loadUrl("http://mobile.mywebsite.com/test/"); 

    } 
    public boolean shouldOverrideUrlLoading(WebView webview, String url) 
    { 
     webview.loadUrl(url); 
     return true; 
    } 


    WebView webView; 
    @Override 
    public void onBackPressed(){ 

     if (webView.isFocused() && webView.canGoBack()) { 
       webView.goBack();  
     } 
     else { 
      new AlertDialog.Builder(this) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .setTitle("Zatvorite aplikaciju?") 
      .setMessage("Da li ste sigurni da želite da izađete?") 
      .setPositiveButton("Da", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        finish(); 
        System.exit(0); 
      } 
     }) 
     .setNegativeButton("Ne", null) 
     .show(); 
    } 

     } 

    public boolean shouldOverrideUrlLoading(webView view, String url) { 
     if(url.startsWith("mailto:")){ 
      MailTo mt = MailTo.parse(url); 
      Intent i = IntentSupport.newEmailIntent(MainActivity.this, mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc()); 
      startActivity(i); 
      view.reload(); 
      return true; 
     } 

      else{ 
       view.loadUrl(url); 
      } 
      return true; 
     } 
    ; 


public static Intent newEmailIntent(Context context, String address, String subject, String body, String cc) { 
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address }); 
    intent.putExtra(Intent.EXTRA_TEXT, body); 
    intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    intent.putExtra(Intent.EXTRA_CC, cc); 
    intent.setType(MIME_TYPE_EMAIL); 
    return intent; 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the currently selected menu XML resource. 
    android.view.MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main, menu); 

    return true; 
} 

/** Called when a menu item in the menu is clicked. */ 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
switch (item.getItemId()) { 
    case R.id.menuabout: 
    Toast.makeText(this, " " + 
     " " + 
     "", Toast.LENGTH_LONG).show(); 
    return true; 

    // Generic catch all for all the other menu resources 
    default: 
    if (!item.hasSubMenu()) 
     return true; 

    break; 
} 

return false; 
} 

    } 

Répondre

0

Vous devez ajouter deux fonctions supplémentaires à votre activité. Vous pouvez les ajouter au bas de votre classe à la fin:

@Override 
public void onStart() { 
    super.onStart(); 
    EasyTracker.getInstance().activityStart(this); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    EasyTracker.getInstance().activityStop(this); 
} 

La fonction onStart() est appelée après onCreate() donc il ne sera pas une incidence sur la mise en œuvre existante. OnStop() est appelé lorsque l'activité est arrêtée afin qu'il attrape des choses comme votre action onBackPressed() pour arrêter l'activité.

Vous pouvez alors choisir d'ajouter un suivi supplémentaire partout dans votre activité en utilisant l'envoi() des fonctions telles que les suivantes:

EasyTracker.getTracker().sendEvent("Group", "Name", obj, value); 

par d'importants non reliés - vous ne devriez pas être appelé System.exit (0) en votre fonction onBackPressed(). S'il s'agit de la seule activité de votre application, l'appel de finish() suffira pour quitter l'application.

+0

Je reçois ceci: La méthode activityStart (MainActivity) n'est pas définie pour le type Object – Emirnop