Android(服务类) - 只有第一次工作getApplicationContext()

时间:2017-07-19 09:08:26

标签: android android-webview

我使用webview和Javascript Interface与Web应用程序进行通信。

例如,我有来自其他项目(工作版)的通知代码。

在OnCreate中定义(静态):

PACKAGE_NAME = getApplicationContext().getPackageName();

PACKAGE_NAME适用于通知方法但不适用于getApplicationContext()。getPackageName()。我已将其替换为已修复。

1)如何为= getApplicationContext()创建变量.getResources()

  if (CURRENT__ == 1) {
            contentNotify =new RemoteViews( PACKAGE_NAME , R.layout.big2);
            contentNotifySmall = new RemoteViews( PACKAGE_NAME , R.layout.small1 );
        }
        else {
            contentNotify =new RemoteViews(PACKAGE_NAME, R.layout.big1);
            contentNotifySmall = new RemoteViews(PACKAGE_NAME, R.layout.small2 );
        }


 icon  = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_launcher);
// NOT EXECUTE CODE FROM HERE (only first time)
 icon = BitmapFactory.decodeResource( getApplicationContext().getResources()  , R.drawable.ic_launcher);

 mBuilder = new NotificationCompat.Builder(this);
 mBuilder.setShowWhen(false);

 etc.

我第一次在onAartCommand的mainActivity和services类中启动服务,我调用了Notification方法。

之后,我从Javascript(JS界面)调用通知方法。

1 个答案:

答案 0 :(得分:1)

Context.getApplicationContext().getResources()会返回Resources个对象,因此您可以将其存储在:

Resources res = getApplicationContext().getResources();