Paste
Pasted as Java by khan ( 14 years ago )
**Main Activity**
package com.example.gcmdemo;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import com.google.android.gcm.GCMRegistrar;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, "160577205208");
Log.e("Msg", "registered");
//Log.e("Msg", "registeredid "+regId);
} else {
Log.e("Msg", "Already registered");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
***********************************************************************MyIntentService*********************************************************
package com.example.gcmdemo;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gcm.GCMBaseIntentService;
public class MyIntentService extends GCMBaseIntentService{
InputStream is = null;
public MyIntentService(){
super(Utils.GCMSenderId);
}
@Override
protected void onError(Context context, String regId) {
// TODO Auto-generated method stub
Log.e("", "error registration id : "+regId);
}
@Override
protected void onMessage(Context context, Intent intent) {
// TODO Auto-generated method stub
handleMessage(context, intent);
}
@SuppressLint("ParserError")
@Override
protected void onRegistered(Context context, String regId) {
// TODO Auto-generated method stub
try{
Log.e("", "registration id : "+regId);
Toast.makeText(context, "hi "+regId,Toast.LENGTH_LONG).show();
handleRegistration(context, regId);
HttpClient httpclient = new DefaultHttpClient();
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpPost httppost = new HttpPost("http://192.168.1.100/aa/cloud.php");
nameValuePairs.add(new BasicNameValuePair("regid", regId));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.e("log_tag", ""+httppost.toString());
if( response == null ){
}else{
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", ""+is.toString());
}
// convert response to string
/*
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
String result = sb.toString();
Log.e("res",result);*/
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
}
@Override
protected void onUnregistered(Context context, String regId) {
// TODO Auto-generated method stub
}
@SuppressWarnings({ "deprecation", "static-access" })
private void handleMessage(Context context, Intent intent) {
// TODO Auto-generated method stub
Utils.notiMsg = intent.getStringExtra("msg");
Utils.notiTitle = intent.getStringExtra("title");
Utils.notiType = intent.getStringExtra("type");
Utils.notiUrl = intent.getStringExtra("url");
int icon = R.drawable.ic_launcher; // icon from resources
CharSequence tickerText = Utils.notiTitle;//intent.getStringExtra("me"); // ticker-text
long when = System.currentTimeMillis(); // notification time
CharSequence contentTitle = ""+Utils.notiMsg; //intent.getStringExtra("me"); // message title
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, tickerText, when);
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, "", pendingIntent);
notification.flags|=notification.FLAG_INSISTENT|notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS;
notification.vibrate=new long[] {100L, 100L, 200L, 500L};
notificationManager.notify(1, notification);
Utils.notificationReceived=true;
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire();
}
private void handleRegistration(Context context, String regId) {
// TODO Auto-generated method stub
Utils.registrationId = regId;
Log.e("", "registration id : "+regId);
}
}
***********************************************************************MyIntentService*******************************************************
****Utils****
package com.example.gcmdemo;
import android.app.Application;
public class Utils extends Application{
public static String GCMSenderId = "160577205208";
public static boolean notificationReceived;
public static String notiTitle="",notiType="",notiMsg="",notiUrl="",registrationId = "";
}
******************
*****menifest********
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gcmdemo"
android:versi
android:versi >
<uses-sdk
android:minSdkVersi
android:targetSdkVersi />
<application
android:ic
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permissi >
<intent-filter>
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.gcmdemo" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcmdemo" />
</intent-filter>
</receiver>
<service android:name="com.example.gcmdemo.MyIntentService" android:enabled="true" />
</application>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.example.gcmdemo.permission.C2D_MESSAGE"
android:protecti />
<uses-permission android:name="com.example.gcmdemo.permission.C2D_MESSAGE" />
</manifest>
Revise this Paste
Children: 52627