Android (Service class) - 只有第一次有效 getApplicationContext()
Android (Service class) - Only first time works getApplicationContext()
我使用 webview 和 Javascript 界面与网络应用程序通信。
例如,我有来自其他项目(工作版本)的通知代码。
在 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.
我第一次在 mainActivity 和服务 class onStartCommand 中启动服务时,我调用了 Notification 方法。
之后,我从Javascript(JS接口)调用通知方法。
Context.getApplicationContext().getResources()
returns 一个 Resources 对象,因此您可以将其存储在其中:
Resources res = getApplicationContext().getResources();
我使用 webview 和 Javascript 界面与网络应用程序通信。
例如,我有来自其他项目(工作版本)的通知代码。
在 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.
我第一次在 mainActivity 和服务 class onStartCommand 中启动服务时,我调用了 Notification 方法。
之后,我从Javascript(JS接口)调用通知方法。
Context.getApplicationContext().getResources()
returns 一个 Resources 对象,因此您可以将其存储在其中:
Resources res = getApplicationContext().getResources();