更新:2013/04/01
發現有很多人搜尋到這篇,C2DM已經更名為GCM(Google Cloud Messaging for Android)
幫大家到找一個比較詳細的說明及操作,大家參考吧
參考資料:
在C2DM 需要到官方去註冊你的App
比較重要
- 1.Package name of your Android app *
- Role (sender) account email *
<!-- Only this application can receive the messages and registration result --> <permission android:name="com.example.myapp.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE" /> <!-- This app has permission to register and receive message --> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <!-- Send the registration id to the server --> <uses-permission android:name="android.permission.INTERNET" />
<service android:name=".C2DMReceiver" />
<!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it --> <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <!-- Receive the actual message --> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.example.myapp" /> </intent-filter> <!-- Receive the registration id --> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.example.myapp" /> </intent-filter> </receiver>
package your.package; import java.io.IOException; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import com.google.android.c2dm.C2DMBaseReceiver; public class C2DMReceiver extends C2DMBaseReceiver{ private static final String TAG="C2DMReceiver"; public C2DMReceiver() { super(C2DMActivity.SENDER_ID); } public C2DMReceiver(String senderId) { super(senderId); // TODO Auto-generated constructor stub } @Override protected void onMessage(Context context, Intent intent) { // TODO Auto-generated method stub Log.v(TAG, "C2DMReceiver message"); } } @Override public void onError(Context context, String errorId) { // TODO Auto-generated method stub Log.v(TAG, "C2DMReceiver error"); } @Override public void onRegistered(Context context, String registrationId) throws IOException { // TODO Auto-generated method stub super.onRegistered(context, registrationId); Log.v(TAG, "C2DMReceiver Register:"+registrationId); } @Override public void onUnregistered(Context context) { // TODO Auto-generated method stub super.onUnregistered(context); Log.v(TAG, "C2DMReceiver UnRegister"); } }
@Override protected void onMessage(Context context, Intent intent) { // TODO Auto-generated method stub Log.v(TAG, "C2DMReceiver message"); Bundle extras = intent.getExtras(); if(extras!=null){ String msg = (String)extras.get(C2DMActivity.MESSAGE_KEY_ONE); Log.v(TAG, "The received msg = "+msg); NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, msg, System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, C2DMActivity.class), 0); notification.setLatestEventInfo(this, getString(R.string.app_name), msg, contentIntent); notificationManager.notify(0, notification); } }
public class C2DMActivity extends Activity { /** Called when the activity is first created. */ //Debugging private static final String TAG = "C2DM"; private static final boolean D = true; public static final String SENDER_ID = "your email"; //使用C2DM服務的用戶帳號 public static final String MESSAGE_KEY_ONE = "message"; //與Server接收消息的key值 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.v(TAG, "Start"); C2DMessaging.register(this, SENDER_ID); //register } }
WifiConfiguration config = new WifiConfiguration(); config.allowedAuthAlgorithms.clear(); config.allowedGroupCiphers.clear(); config.allowedKeyManagement.clear(); config.allowedPairwiseCiphers.clear(); config.allowedProtocols.clear(); config.SSID = "\"" + SSID + "\"";
config.wepKeys[0] = ""; config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0;
config.preSharedKey = "\""+Password+"\""; config.hiddenSSID = true; config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.wepTxKeyIndex = 0;
config.preSharedKey = "\""+Password+"\""; config.hiddenSSID = true; config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); config.status = WifiConfiguration.Status.ENABLED;
mWifiManager = (WifiManager)getSystemService(WIFI_SERVICE); try { WifiConfiguration wc = new WifiConfiguration(); wc.SSID = "\"ICCL\""; //AP SSID NAME wc.preSharedKey = "\"passwd\""; //AP passwd wc.hiddenSSID = true; wc.status = WifiConfiguration.Status.ENABLED; wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); int netId = mWifiManager.addNetwork(wc); boolean success = mWifiManager.enableNetwork(netId, false); //Connect AP if(success){ //只能說明你的密碼沒有輸錯,並且網路可用,但不一定連接上了! Toast.makeText(getApplicationContext(), "Connect ICCL AP.", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplicationContext(), "Connect ICCL AP.", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Toast.makeText(getApplicationContext(), "Err", Toast.LENGTH_SHORT).show(); e.printStackTrace(); }
cd /usr/ports/ftp/php5-curl make install clean
Alias /phpmyadmin "/usr/local/www/phpMyAdmin/" <Directory "/usr/local/www/"> Options none AllowOverride Limit Order allow,deny Allow from all </Directory>