I want to add a service to my manifest but it gives a warning in.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aura_apps.tygo_asbroek.intentexample" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".TygoIntentService"
>
</service>
</application>
Here is the Java code:
package com.aura_apps.tygo_asbroek.intentexample;
public class TygoIntentService extends IntentService {
private static final String TAG = "com.bluelionapps.intentexample";
public TygoIntentService(String name) {
super("TygoIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
//This is what the service does
Toast toast = Toast.makeText(getApplicationContext(), "Service Lauched", Toast.LENGTH_SHORT);
toast.show();
}
}
Here you see my complete Manifest but .TygoIntentService says TygoIntentService.java doesn't have a default constructor of my app. If i run it, it's just working but I want to know if this can give any problems in the future and if it gives one what i need to do.
Remove String name parameter from the constructor:
public TygoIntentService() {
super("TygoIntentService");
}
If you do not use/need the name String there, just remove the constructor, and let the compiler add it behind scenes by default.
Related
I have been trying to launch my app after boot completed, but somehow it works on virtual devices but not working on real devices. It is working on from till android 8 but not from android 9 in devices like Xiaomi, Oneplus and Realme. I have read the documents, it was stated that developers can still use receive_boot_completed. I am not able to solve this problem any help will be appreciated.
Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.restartexample">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".RestartReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
</manifest>
RestartReceiver:
public class RestartReceiver extends BroadcastReceiver {
private static String TAG = RestartReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Receiver","onReceive");
StringBuilder sb = new StringBuilder();
sb.append("Action: " + intent.getAction() + "\n");
sb.append("URI: " + intent.toUri(Intent.URI_INTENT_SCHEME).toString() + "\n");
String log = sb.toString();
Log.d(TAG, log);
Toast.makeText(context, log, Toast.LENGTH_LONG).show();
}
}
Sorry if this one is a stupid question.
I am trying to do this Android global variable
I added a new class but I cannot call the methods inside the class. I get:
inconvertible types; cannot cast 'android.app.Application' to 'com.app.android.appname.classname'
here is the code inside the new class:
public class GlobalVars extends Application {
private static int lvl;
public int getLvl() {
return lvl;
}
public void setLvl(int lvl) {
lvl = this.lvl;
}
}
my manifest is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.android.guessinggame">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
android:screenOrientation="sensorLandscape"
<activity android:name=".MainActivity">
android:screenOrientation="sensorLandscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".enterword"></activity>
<activity android:name=".player1turn"></activity>
<activity android:name=".aiturn"></activity>
<activity android:name=".chooselevel"></activity>
<application android:name=".GlobalVars"/>
</application>
</manifest>
then in the activity:
protected void select1 (View view) {
((GlobalVars) this.getApplication()).setLvl(1); <-------- error
Intent intent = new Intent(this, enterword.class);
startActivity(intent);
}
this produces:
inconvertible types; cannot cast 'android.app.Application' to 'com.app.android.guessinggame.GlobalVars'
Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.app.android.guessinggame.GlobalVars
Solved
in the manifest, the name must be defined. not add. changed it to:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.android.guessinggame">
<application
android:name=".GlobalVars"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
android:screenOrientation="sensorLandscape"
<activity android:name=".MainActivity">
android:screenOrientation="sensorLandscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".enterword"></activity>
<activity android:name=".player1turn"></activity>
<activity android:name=".aiturn"></activity>
<activity android:name=".chooselevel"></activity>
</application>
</manifest>
now it works.
If you want to make GlobalVars your Application class in Android you have to make it extend android.app.Application.
You also have to declare this in the Manifest.
On a side note, there is only one instance of Application. You may not need to make lvl static.
public class GlobalVars extends Application{
private static int lvl;
public int getLvl() {
return lvl;
}
public void setLvl(int lvl) {
lvl = this.lvl;
}
}
In the manifest you have to have:
<application
android:name=".GlobalVars"
...
</application>
In your activity, in onCreate() for example:
((GlobalVars) getApplication()).getLvl();
I want to get the called number in android but When I start the outgoing call it fails I am using broadcast receiver and register it in service to keep listen if activity not in focus here is my code.
Menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vampirepc.androidservice" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.BOOT_COMPLETED">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</action>
</intent-filter>
</activity>
<service android:name=".MyService" />
<receiver
android:name="com.example.vampirepc.androidservice.OutgoingReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest>
BroadcastReceiver Class
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(ctx,
"Inside broadcast",
Toast.LENGTH_LONG).show();
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show();
}
Service
#Override
public void onCreate() {
Toast.makeText(getApplicationContext(),"Service created",Toast.LENGTH_SHORT).show();
try {
IntentFilter filter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");
OutgoingReceiver myReceiver = new OutgoingReceiver();
registerReceiver(myReceiver, filter);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"Exception is "+String.valueOf(e),Toast.LENGTH_SHORT).show();
}
}
Have a look at this
Phone state Broadcaster
Please add the permission in the top ,this helped me
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vampirepc.androidservice" >
android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.BOOT_COMPLETED">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</action>
</intent-filter>
</activity>
<receiver
android:name="com.example.vampirepc.androidservice.OutgoingReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<service android:name=".MyService" />
</application>
<uses-permission
</manifest>
Have you realized in this line:
Toast.makeText(ctx,
"Inside broadcast",
Toast.LENGTH_LONG).show();
You used ctx instead of context.
Thank to all i have solved the problem my self I was using incorrect context in toaster the correct onReceive is.
I was using ctx which was initialize.
Context ctx;
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "this is not shown" , Toast.LENGTH_LONG).show();
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show();
}
I have tried this on a few android phones with no luck (i did start the app once).
This was mostly generated from android studio 1.3.2 and from some questions like this and that and the other.
Perhaps someone can point out what i am doing wrong.
Thanks
edit: moved receiver inside app, but no joy.
edit2: added missing permissions for receiver. now seems to work on a nexus 4. but not on the kindle fire or the at&t tablets. although it seems to come right away if i press the circle icon on the azpen.
edit3: seems to work sometimes on the fires now.
package acme.startup;
import android.content.*;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}
}
package acme.startup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="acme.startup" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Every Component must be declared inside application tag into manifest. Here you declared receiver outside of application tag. Read Structure of the Manifest File.
Correct manifest wil be
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="acme.startup" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
You have to add permission to manifest file. Add this before your <application> tag :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
I made a class that will hold application wide variables. I made it extend activity and even included it in the manifest but I'm still getting this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.blacksmith/com.example.blacksmith.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.blacksmith.GlobalVars
Here is the class:
package com.example.blacksmith;
import android.app.Application;
public class GlobalVars extends Application {
private Boolean DefaultEncrypt;
private String Password;
public GlobalVars(){
DefaultEncrypt = true;
}
public void setDefaultEncrypt(Boolean is){
DefaultEncrypt = is;
}
public Boolean getDefaultEncrypt(){
return DefaultEncrypt;
}
public void setPassword(String pass){
Password = pass;
}
public String getPassword(){
return Password;
}
}
Here is the android manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blacksmith"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="Blacksmith"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PasswordCreatorActivity"
android:label="#string/title_activity_password_creator" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
</application>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name=".application.GlobalVars">
</application>
</manifest>
This is how I'm accessing it:
GlobalVars tmp = ((GlobalVars) getApplicationContext());
tmp.setDefaultEncrypt(sharedPref.getBoolean("Default", true));
tmp.setPassword(sharedPref.getString("password", "0x1234"));
Note: I tried this.getApplication() and getApplication() and neither worked
What am I doing wrong and how do I fix it.
Thanks ahead of time.
I am not 100% sure, but you have 2 application stanzas, try moving/replacing the items in :
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name=".application.GlobalVars">
</application>
into your original application stanza :
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name=".application.GlobalVars"
>
<activity
android:name=".MainActivity"
android:label="Blacksmith"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PasswordCreatorActivity"
android:label="#string/title_activity_password_creator" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
</application>