本文介绍了Android的C2DM编程示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid应用程序的开发。我正在开发使用C2DM一个Android应用程序。谁能告诉我如何获得注册ID从C2DM的应用程序。请告诉我一个详细的例子和说明

解决方案

 公共类C2dmEx扩展活动
{


静态的TextView MYTEXT = NULL;
上下文的背景下= NULL;
意向意图= NULL;



@覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    MYTEXT =(TextView中)findViewById(R.id.mytext);
    mytext.setText(应用程序启动);


    意图registrationIntent =新的意向书(com.google.android.c2dm.intent.REGISTER);
    registrationIntent.putExtra(应用程序,PendingIntent.getBroadcast(此,0,新意图(),0));
    registrationIntent.putExtra(发件人,你的邮件ID);
    startService(registrationIntent);

    mytext.setText(抓住你的设备注册ID ......);

    Log.i(接受她,1);

  }
}



公共类C2DMReceiver扩展的BroadcastReceiver
{

//私人CustomerBean客户= NULL;
私人RegisterManager章= NULL;
私人C2dmEx EX = NULL;
INT用户ID = 0;
私人布尔AUTH = FALSE;
私人CustLogin CL = NULL;

公共C2DMReceiver()
{

    CL =新CustLogin();
}

@覆盖
公共无效的onReceive(上下文的背景下,意图意图)
    {
        Log.i(接受她,2);
        C2dmEx.mytext.setText(意图收到!);
        如果(intent.getAction()。等于(com.google.android.c2dm.intent.REGISTRATION))
        {
            handleRegistration(背景下,意图);
        }
        否则,如果(intent.getAction()。等于(com.google.android.c2dm.intent.RECEIVE))
        {
            的handleMessage(背景下,意图);
        }
     }

    私人无效handleRegistration(上下文的背景下,意图意图)
    {

        字符串登记= intent.getStringExtra(registration_id);
        如果(intent.getStringExtra(错误)!= NULL)
        {
           C2dmEx.mytext.setText(有您的设备注册的错误!);
            //注册失败,应稍后重试。
        }
        否则,如果(intent.getStringExtra(未登记)!= NULL)
        {
            //注销完成后,从授权发送新邮件将被拒绝
           C2dmEx.mytext.setText(你已被注销!);
        }
        否则,如果(注册!= NULL)
        {
           //发送注册ID的第三方站点的,用于发送的消息。
           //这应在一个单独的线程来实现。
           //完成后,请记住,所有注册完成。

        //用户ID = customer.getId();
        // Log.i(ID,将String.valueOf(用户ID));

            字符串REGID =登记;
            Log.i(章,将String.valueOf(REGID));
            C2dmEx.mytext.setText(您的注册code是:+ REGID);





        }
    }

    私人无效的handleMessage(上下文的背景下,意图意图)
    {
        C2dmEx.mytext.setText(你已被惊动!);
    }


}
 

和您的清单

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.mmp.geopon
    安卓版code =1
    机器人:VERSIONNAME =0.1>

    <应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>

        <活动机器人:名称=。C2dmEx
                  机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;

    <接收机器人:C2DMReceiver名称=机器人:权限=com.google.android.c2dm.permission.SEND>

         <意向滤光器>
                  <作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
                 <类机器人:名称=com.mmp.geopon/>
          &所述; /意图滤光器>

        <意向滤光器>
              <作用机器人:名称=com.google.android.c2dm.intent.REGISTRATION/>
              <类机器人:名称=com.mmp.geopon/>
         &所述; /意图滤光器>

     < /接收器>


< /用途>

<使用-SDK安卓的minSdkVersion =8/>
<允许机器人:名称=com.mmp.geopon.permission.C2D_MESSAGE安卓的ProtectionLevel =签名/>
 <使用-权限的Andr​​oid:名称=com.mmp.geopon.permission.C2D_MESSAGE/>
  <使用-权限的Andr​​oid:名称=com.google.android.c2dm.permission.RECEIVE/>
    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
    <使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/>

< /舱单>
 

I am new to android application development. I am developing an android application using c2dm. Can anyone tell me of how to receive the registration id for the application from c2dm. Please tell me with a detailed example and explanation

解决方案
public class C2dmEx extends Activity
{


static TextView mytext = null;
Context context = null;
Intent intent = null;



@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mytext = (TextView) findViewById(R.id.mytext);  
    mytext.setText("app started");


    Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); 
    registrationIntent.putExtra("sender","your Mail Id");
    startService(registrationIntent);

    mytext.setText("Grabbing your device registration ID.....");

    Log.i("Recieve","1");

  }
}



public class C2DMReceiver extends  BroadcastReceiver
{

//private CustomerBean customer = null;
private RegisterManager reg = null;
private C2dmEx ex = null;
int UserId = 0;
private boolean auth = false;
private CustLogin cl = null;

public C2DMReceiver()
{

    cl = new CustLogin();
}

@Override
public void onReceive(Context context, Intent intent)
    {
        Log.i("Recieve","2");
        C2dmEx.mytext.setText("Intent Received!");
        if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION"))
        {
            handleRegistration(context, intent);
        } 
        else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) 
        {
            handleMessage(context, intent);
        }
     }

    private void handleRegistration(Context context, Intent intent)
    {

        String registration = intent.getStringExtra("registration_id"); 
        if (intent.getStringExtra("error") != null)
        {
           C2dmEx.mytext.setText("There was an error with your device registration!");
            // Registration failed, should try again later.
        } 
        else if (intent.getStringExtra("unregistered") != null)
        {
            // unregistration done, new messages from the authorized sender will be rejected
           C2dmEx.mytext.setText("You have been unregistered!");
        } 
        else if (registration != null) 
        {
           // Send the registration ID to the 3rd party site that is sending the messages.
           // This should be done in a separate thread.
           // When done, remember that all registration is done.

        //  UserId = customer.getId();
        //  Log.i("id",String.valueOf(UserId));

            String RegId = registration; 
            Log.i("reg",String.valueOf(RegId) );
            C2dmEx.mytext.setText("Your registration code is: " + RegId);





        }
    }

    private void handleMessage(Context context, Intent intent)
    {
        C2dmEx.mytext.setText("You have been alerted!");
    }


}

and your manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mmp.geopon"
    android:versionCode="1" 
    android:versionName="0.1">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".C2dmEx"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND">

         <intent-filter>
                  <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                 <category android:name="com.mmp.geopon" />
          </intent-filter>

        <intent-filter>
              <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
              <category android:name="com.mmp.geopon" />
         </intent-filter>

     </receiver>


</application>

<uses-sdk android:minSdkVersion="8" />
<permission android:name="com.mmp.geopon.permission.C2D_MESSAGE" android:protectionLevel="signature" />
 <uses-permission android:name="com.mmp.geopon.permission.C2D_MESSAGE" />
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

</manifest>

这篇关于Android的C2DM编程示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 06:14