android c2dm编程示例

发布于 2024-10-16 09:50:52 字数 93 浏览 1 评论 0原文

我是 Android 应用程序开发新手。我正在使用 c2dm 开发 Android 应用程序。谁能告诉我如何从 c2dm 接收应用程序的注册 ID。请告诉我详细的例子和解释

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花似海 2024-10-23 09:50:52
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!");
    }


}

和你的清单

<?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>
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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文