我们如何以编程方式更改活动主题

时间:2013-09-04 08:10:55

标签: android android-intent android-notifications

我创建了一个Android app.I希望当它安装在设备中时,它将不显示任何GUI部分。当我们将从服务器广播消息时,在选择通知时我希望用户显示GUI部分。我们如何才能做到这一点Android

我在我的Notification函数中尝试了这一点,我将开始新的Intent

 setTheme(android.R.style.Theme);

这在我的 Manifest.XML 文件中

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.iween.gcmclient" android:versionCode="1" android:versionName="1.0">


    <uses-sdk 
        android:minSdkVersion="8" 
        android:targetSdkVersion="17"/>

    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <permission 
        android:name="com.example.iween.gcmclient.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission 
        android:name="com.example.iween.gcmclient.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive data message. -->
    <uses-permission
        android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!-- Main activity. -->
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.iween.gcmclient.DemoActivity"
            android:label="@string/app_name"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:launchMode="singleTop">
            <intent-filter>
                <action 
                    android:name="android.intent.action.MAIN" />
            <category 
                android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver
            android:name="com.example.iween.gcmclient.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.google.android.gcm.demo.app" />
            </intent-filter>
        </receiver>
        <service android:name="com.example.iween.gcmclient.GcmIntentService" />
    </application>

</manifest>

通知功能

private void sendNotification(String recivedMessage) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, DemoActivity.class), 0);
       NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_stat_gcm)
        .setContentTitle("Iween Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(recivedMessage))
        .setContentText(recivedMessage);

        mBuilder.build().flags = Notification.FLAG_AUTO_CANCEL;
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(notificationSound);
        mBuilder.setAutoCancel(true);
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

1 个答案:

答案 0 :(得分:0)

试试这种方式

创建一个公共static int变量,该变量将在收到通知时更新

例如在您的接收器类中

public static int THEME = 0;
onReceive(){

  if(cond1){
       THEME = R.style.them1;
  }else if(cond2){
       THEME = R.style.them2;
  }

}




@Override
    protected void onCreate(Bundle savedInstanceState) {

              if(THEME!=0){
                 setTheme(THEME);
              }

        super.onCreate(savedInstanceState);