2016-04-28 8 views
1

Hier meine Dienstklasse istIntent Dienst nicht genannt

package com.example.com.listener; 

import android.app.DownloadManager; 
import android.app.IntentService; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 
import android.support.v4.content.LocalBroadcastManager; 
import android.util.Log; 
import android.widget.Toast; 

import com.android.volley.Request; 
import com.google.android.gms.gcm.GoogleCloudMessaging; 
import com.google.android.gms.iid.InstanceID; 

import java.io.IOException; 
import java.util.HashMap; 
import java.util.Map; 

import com.example.com.R; 
import com.example.com.listener.GCMRegistrationListener; 
import com.example.com.util.Constants; 
import com.example.com.web.RawRequest; 
import com.example.com.web.WebRequest; 

public class GCMRegistrationService extends IntentService{ 

    private static final String TAG = "GCMRegistrationService"; 
    private static final String[] TOPICS = {"global"}; 

    public GCMRegistrationService() { 
     super(TAG); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 

     try { 

      InstanceID instanceID = InstanceID.getInstance(this); 
      String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); 
      sendRegistrationToServer(token,sharedPreferences.getString(Constants.EMAIL_SHARED_PREF,"")); 
      Toast.makeText(this,"This is your token " + token,Toast.LENGTH_LONG).show(); 
      Log.i("token", token); 
      // You should store a boolean that indicates whether the generated token has been 
      // sent to your server. If the boolean is false, send the token to your server, 
      // otherwise your server should have already received the token. 
      sharedPreferences.edit().putBoolean(Constants.GCM_TOKEN_TO_SERVER, true).apply(); 
      // [END register_for_gcm] 
     } catch (Exception e) { 
      Log.d(TAG, "Failed to complete token refresh", e); 
      // If an exception happens while fetching the new token or updating our registration data 
      // on a third-party server, this ensures that we'll attempt the update at a later time. 
      sharedPreferences.edit().putBoolean(Constants.GCM_TOKEN_TO_SERVER, false).apply(); 
     } 
    } 

    /** 
    * Persist registration to third-party servers. 
    * 
    * Modify this method to associate the user's GCM registration token with any server-side account 
    * 
    * @param token The new token. 
    */ 
    private void sendRegistrationToServer(String token,String email) { 
     WebRequest request= new RawRequest(this,Constants.SEND_GCM_TOKEN, Request.Method.POST); 
     Map<String,String> requestParams= new HashMap<>(); 
     requestParams.put(Constants.GCM_TOKEN_LABEL,token); 
     requestParams.put(Constants.EMAIL_SHARED_PREF,email); 
     request.sendRequest(new GCMRegistrationListener(), requestParams, null); 

    } 

} 

Hier ist der Code, den ich verwendet, um den Dienst zu starten:

Intent gcmRegister= new Intent(context,GCMRegistrationService.class); 
       context.startService(gcmRegister); 

Manifest-Datei der Eintrag: Manifest Paket ist com.example. com

und Service-Eintrag ist

<service android:name=".listener.GCMRegistrationService" android:exported="false" /> 

Leider läuft mein Dienst nicht. Bitte helfen Sie.

+0

bitte teilen Sie die Tag (wegen der Verpackung) – 0X0nosugar

+0

Informationen in Frage. –

+0

Ist der Dienst in einem Unterpaket (Listener) von com.example.com? – 0X0nosugar

Antwort

0

Ihre Daten ändern entsprechend Ihrer Anforderung, können diesen Service-Tag außerhalb des Schließens Aktivitäts-Tag in Manifest-Datei:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.twitter" 
    android:versionCode="1" 
    android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="15" /> 
<uses-permission android:name="android.permission.INTERNET"/> 
<application android:icon="@drawable/ic_launcher"    
     android:label="@string/app_name" > 
    <activity 
     android:name=".TwitterTwoActivity" 
     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="tfe.rma.ciss.be.Server"/> 

</application> 
</manifest> 

zu onCreate super.onCreate() und ändern onStart von onStartCommand (plus ihrer Super Hinzufügen Super super.onStartCommand().

package tfe.rma.ciss.be; 

import org.xml.sax.Attributes; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 
import org.xml.sax.XMLReader; 
import org.xml.sax.helpers.DefaultHandler; 
import org.xmlpull.v1.XmlPullParserException; 
import org.xmlrpc.android.MethodCall; 
import org.xmlrpc.android.XMLRPCServer; 

import android.app.IntentService; 
import android.content.Intent; 
import android.util.Log; 
import android.widget.Toast; 

import java.io.IOException; 
import java.io.StringReader; 
import java.net.MalformedURLException; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.ArrayList; 

import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 

public class Server extends IntentService { 
    public String myData=""; 
    public String streamTitle = "",path=""; 

    public void onCreate() { 
     super.onCreate(); 
     Log.d("Server", ">>>onCreate()"); 
    } 

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

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     super.onStartCommand(intent, startId, startId); 
     Log.i("LocalService", "Received start id " + startId + ": " + intent); 

     return START_STICKY; 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Log.d("Server", ">>>handlingIntent()"); 
     try { 
      ServerSocket socket = new ServerSocket(8214); 
      XMLRPCServer server = new XMLRPCServer(); 
      Log.d("Server", ">>>opening on port" + socket); 

      while (true) { 
       Socket client = socket.accept(); 
       MethodCall call = server.readMethodCall(client); 
       String name = call.getMethodName(); 

       if (name.equals("newImage")) { 
        ArrayList<Object> params = call.getParams(); 
        // assume "add" method has two Integer params, so no checks done 
        myData = (String)(params.get(0)); 
        //int i1 = (Integer) params.get(1); 
        server.respond(client, new Object[] {200}); 
        /*intent = new Intent (this, ParseFunction.class); 
        startService (intent); */ 

        Toast.makeText(this, myData, Toast.LENGTH_SHORT).show(); 
        Log.d("ParseFunction", ">>>Started()"); 

        Intent i = new Intent(this, B.class); 
        i.putExtra("Azo", myData); 
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(i); 
       } else { 
        server.respond(client, null); 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (XmlPullParserException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

Erklären, was Sie geändert haben und warum ist essentielle Informationen –

+0

er schreibt falsche Paketnamen, wie Sie seinen Paketnamen in Manifest –

+0

sehen können jetzt bearbeitet er den Paketnamen –

0

GCM-Register() ist veraltet. verwenden InstanceID allgemeines GCM Registrierungsmanagement durchzuführen. wie unten gezeigt, die Verwendung von google lib spielen, es zu bekommen.

Log.i(TAG, "GCM Registration event is received"); 
    String token; 

    try { 
     token = InstanceID.getInstance(context).getToken(context.getString(R.string.gcm_defaultSenderId), 
       GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); 
     Log.i(TAG, "GCM Registration Token retrieved from google cloud: " + token); 
     // 
    } catch (IOException e) { 
     // exponential back off 
     Log.e(TAG, "IO error occurred, going to back off. backoffIndexGCM:" + backoffIndexGCM); 
     exponentialBackOff.postDelayed(exponentialBackOffGCMRegisterRun, 1000 * backoffIndexGCM); 
     backoffIndexGCM *= 2; 
     // should not wait more than 1000 sec in any circumstance. 
     backoffIndexGCM %= 1000; 
    } 
+0

Ich verwende bereits InstanceID. Bitte überprüfen Sie das Skript. –

Verwandte Themen