Android项目,数据库(服务器),初学者

时间:2016-03-01 14:44:02

标签: android layout

我正在尝试创建一个应用程序,我可以向已安装我的应用程序的每个人全局广播新闻。让我们举一个例子:假设有一条维护道路(在A点和B点之间)。现在,我将此消息保存到服务器上,以便所有安装了该应用程序的人都可以看到该消息。我该怎么做?谁能告诉我如何开始?

3 个答案:

答案 0 :(得分:0)

推荐: Google推送为您完成工作。

不推荐: 或者您可以开发自己的服务,检查服务器是否有新消息,但是您需要每隔X分钟或几秒检查一次(这会导致CPU和RAM过载,因为知道Google Push也在为其他应用程序执行此操作

答案 1 :(得分:0)

您实际上寻找的是推送通知。 GCM推送通知。

这里是BroadcastReceiver,所以我不发布单行答案:

 /**
 * This {@code WakefulBroadcastReceiver} takes care of creating and managing a
 * partial wake lock for your app. It passes off the work of processing the GCM
 * message to an {@code IntentService}, while ensuring that the device does not
 * go back to sleep in the transition. The {@code IntentService} calls
 * {@code GcmBroadcastReceiver.completeWakefulIntent()} when it is ready to
 * release the wake lock.
 */

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // Explicitly specify that GcmIntentService will handle the intent.
    ComponentName comp = new ComponentName(context.getPackageName(),
            GCMIntentService.class.getName());
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

这是IntentService

/**
 * This {@code IntentService} does the actual handling of the GCM message.
 * {@code GcmBroadcastReceiver} (a {@code WakefulBroadcastReceiver}) holds a
 * partial wake lock for this service while the service does its work. When the
 * service is finished, it calls {@code completeWakefulIntent()} to release the
 * wake lock.
 */
public class GCMIntentService extends IntentService {

protected void onHandleIntent(Intent intent) {
    if(intent == null) {
        return;
    }
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    // The getMessageType() intent parameter must be the intent you received
    // in your BroadcastReceiver.
    String messageType = gcm.getMessageType(intent);

    if (extras != null && !extras.isEmpty()) {  // has effect of unparcelling Bundle
        /*
         * Filter messages based on message type. Since it is likely that GCM will be
         * extended in the future with new message types, just ignore any message types you're
         * not interested in, or that you don't recognize.
         */
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification(extras);
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification(extras);
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            sendNotification(extras);
            Log.i(LOGTAG, "Received EXTRAS bundle: " + extras.toString());
        }
    }
    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GCMBroadcastReceiver.completeWakefulIntent(intent);
}
    private void sendNotification(Bundle msgBundle) {

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, splashIntent, PendingIntent.FLAG_UPDATE_CURRENT);

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.dpd_notification_icon).setAutoCancel(true).setContentTitle(getResources().getString(R.string.app_name)).setStyle(new NotificationCompat.BigTextStyle().bigText(current.msg)).setContentText(current.msg);
                mBuilder.setContentIntent(contentIntent);
                mBuilder.setVibrate(pattern);

                Notification note = mBuilder.build();
                note.vibrate = pattern;

                if(note != lastNotification) {
                    mNotificationManager.notify(NOTIFICATION_ID, note);
                    lastNotification = note;
                }

                Intent notificationMsg = new Intent(Constants.NOTIFICATION_MESSAGE);
                notificationMsg.putExtra(Constants.NOTIFICATION_MESSAGE_TEXT, current.msg);
                notificationMsg.putExtra(Constants.PARCEL_UPDATE_SILENT, current.silent);
                notificationMsg.putExtra(Constants.NOTIFICATION_ID, NOTIFICATION_ID);
                        sendBroadcast(notificationMsg);

            }

            NOTIFICATION_ID++;
        }

您在AndroidManifest.xml中需要以下权限     

<permission
    android:name="${applicationId}.permission.C2D_MESSAGE"
    android:protectionLevel="signature"/>

<uses-permission android:name="${applicationId}.permission.C2D_MESSAGE"/>

以及

    <receiver
        android:name=".backend.push.GCMBroadcastReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
            <category android:name="my.package.name"/>
        </intent-filter>
    </receiver>
    <service android:name=".backend.push.GCMIntentService"/>

答案 2 :(得分:-1)

首先,您需要了解什么是HTTP&#39;。它可以连接您的应用和服务器。(您最好了解一下TCP / IP。它们是Http的基础。)

然后,也许你可以了解一些关于&#39;线程&#39;和&#39;处理&#39;。

将新闻推送到您的服务器,然后让您的应用获取它们。