Reputation: 4931
my program flow is
my problem is at 5 : "Activity instance is null"
my code is
public class MainActivity extends Activity {
static MainActivity Iinstance;
private String ServiceName = myService.class.getCanonicalName();
ServiceStatus servicestatus = new ServiceStatus();
static Handler handler = new Handler();
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(
LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView1);
Iinstance = this;
Button B;
//there is clicklistener for B to start service.
}
class AA{
void A(String html){
tv.setText(html);
}
}
and service code is
public class myService extends Service {
//fields
public int onStartCommand(Intent i, int flags, int startId) {
//code to start TimerTask.
}
//register the receiver.
CancelReceiver receiver = new CancelReceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("MY_ACTION");
registerReceiver(receiver, intentFilter);
return START_REDELIVER_INTENT;
}
class task extends TimerTask{
@Override
public void run() {
try {//get html from webviewClient
handlers.post(new Runnable(){
@Override
public void run() {
//webView load URL.I have two doubts around this.
WebView webView = new WebView(myService.this);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new HogeWebViewClinet());
webView.addJavascriptInterface(new WebViewLogger(), "webViewLogger");
String Url = (String) map.get("URL");
webView.loadUrl(Url);
}
});
}
}
class HogeWebViewClinet extends WebViewClient {
@Override public void onPageFinished(WebView view, String url) {
//throw the html from webview to "class WebViewLogger"as "String str"by loading javascript.
view.loadUrl("javascript:window.webViewLogger.log(document.documentElement.outerHTML);");
}
}
class WebViewLogger {
public void log(String str) {
Intent broadcastIntent = new Intent();
broadcastIntent.putExtra("Status", str);
broadcastIntent.setAction("MY_ACTION");
sendBroadcast(broadcastIntent);
}
}
}
then go to broadcast class. public class Receiver extends BroadcastReceiver {
String serviceResult = null;
@Override
public void onReceive(Context arg0, Intent intent) {
AA aa = MainActivity.Iinstance.new AA(); //threw error in this line.
aa.a(intent.getStringExtra(Status);
}
that's all.
why"MainActivity.Iinstance.new AA();"is null?
my guess why activity getting null is
after all, i couldnt solved this however i tried many solution.
please help me.
thanks.
Upvotes: 1
Views: 957
Reputation: 3636
You cannot keep a static variable of your activity, that would lead to leaks and bugs!
Your activity will be re-created and removed by the Android system depending on various conditions (needing the memory resources, configuration change, etc.)
You should implement communication between your Activity and your Service using Intent.
Set you activity launchMode
to singleTop
if you don't want to create a new activity each time. You would then need to handle the intent in the onNewIntent(Intent)
method of your activity
You cannot use a webView inside a service. You have to use a class like HttpUrlConnection
. See Connect and Download Data for more information
Upvotes: 1