Click here to Skip to main content
15,884,388 members
Home / Discussions / Android
   

Android

 
GeneralRe: Android Day of Week Calculator Pin
Richard MacCutchan9-Feb-17 21:07
mveRichard MacCutchan9-Feb-17 21:07 
GeneralRe: Android Day of Week Calculator Pin
Pavlex49-Feb-17 21:39
Pavlex49-Feb-17 21:39 
GeneralRe: Android Day of Week Calculator Pin
Richard MacCutchan9-Feb-17 22:20
mveRichard MacCutchan9-Feb-17 22:20 
GeneralRe: Android Day of Week Calculator Pin
David Crow10-Feb-17 2:08
David Crow10-Feb-17 2:08 
GeneralRe: Android Day of Week Calculator Pin
Richard MacCutchan10-Feb-17 2:46
mveRichard MacCutchan10-Feb-17 2:46 
GeneralMobile and o/s application development Pin
JimmiJames337-Feb-17 5:42
professionalJimmiJames337-Feb-17 5:42 
QuestionRe: Mobile and o/s application development Pin
David Crow7-Feb-17 6:15
David Crow7-Feb-17 6:15 
QuestionAndroid Popup Window Pin
Pavlex42-Feb-17 8:09
Pavlex42-Feb-17 8:09 
I have created app that detects otg cable using service. I have created button in action bar with icon,how to make if otg cable is connected hide that button if not connected it should show text and when clicked on it to get popup window with text and animation?

MainActivity.class

Java
public class MainActivity extends AppCompatActivity
{

    public void startOtgService()
    {
        startService(new Intent(MainActivity.this, OtgService.class));

    }

    public void stopOtgService()
    {
        stopService(new Intent(MainActivity.this, OtgService.class));

    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        Button startButton = (Button)this.findViewById(R.id.startButton);
        Button stopButton = (Button)this.findViewById(R.id.stopButton);

        startButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                startOtgService();
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                stopOtgService();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }
        if(id ==  R.id.action_cable)
        {

        }
        return super.onOptionsItemSelected(item);
    }
}


OtgService.class

Java
public class OtgService extends Service
{

    private boolean mOtgConnected = false;
    private Handler mHandler;

    Timer taskTimer = new Timer();

    TimerTask task = new TimerTask()
    {
        private int last_Length = -1;

        @Override
        public void run()
        {
            Context context = OtgService.this.getBaseContext();

            File directory = new File("/sys/bus/usb/devices");
            File[] contents = directory.listFiles();

            int conn_length = contents.length;

            if(conn_length ==last_Length)
            {
                return;
            }
            else
            {
                last_Length = conn_length;
            }

            if(conn_length == 0)
            {
                mOtgConnected = false;
                mHandler.post(flagChangedTask);
            }
            else if (conn_length > 0)  //Might get a -1
            {
                mOtgConnected = true;
                mHandler.post(flagChangedTask);

            }

        }
    };

    Runnable flagChangedTask = new Runnable()
    {
        @Override
        public void run()
        {
            if (mOtgConnected)
                Toast.makeText(OtgService.this,"otg connected",Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(OtgService.this,"otg not connected",Toast.LENGTH_SHORT).show();
        }
    };


    public OtgService()
    {

    }


    private void onStartCompat(Intent intent)
    {
        Log.e("###", "Starting service!");

        if (mHandler == null)
            mHandler = new Handler(getMainLooper());

        taskTimer.scheduleAtFixedRate(task, 0, 1000);
    }

    @Override
    public void onStart(Intent intent, int startId)
    {
        onStartCompat(intent);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        onStartCompat(intent);
        return START_STICKY;
    }

    @Override
    public void onDestroy()
    {
        //task.cancel();
    }


    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
}

QuestionPermissions Not Persistent During Across App Startups Pin
Django_Untaken1-Feb-17 20:51
Django_Untaken1-Feb-17 20:51 
AnswerRe: Permissions Not Persistent During Across App Startups Pin
Afzaal Ahmad Zeeshan1-Feb-17 22:33
professionalAfzaal Ahmad Zeeshan1-Feb-17 22:33 
GeneralRe: Permissions Not Persistent During Across App Startups Pin
Django_Untaken2-Feb-17 1:06
Django_Untaken2-Feb-17 1:06 
GeneralRe: Permissions Not Persistent During Across App Startups Pin
Afzaal Ahmad Zeeshan2-Feb-17 2:21
professionalAfzaal Ahmad Zeeshan2-Feb-17 2:21 
GeneralRe: Permissions Not Persistent During Across App Startups Pin
Django_Untaken2-Feb-17 2:58
Django_Untaken2-Feb-17 2:58 
GeneralRe: Permissions Not Persistent During Across App Startups Pin
Richard Deeming2-Feb-17 3:46
mveRichard Deeming2-Feb-17 3:46 
QuestionAlarm Manager Pin
Pavlex429-Jan-17 0:40
Pavlex429-Jan-17 0:40 
AnswerRe: Alarm Manager Pin
Richard MacCutchan29-Jan-17 2:01
mveRichard MacCutchan29-Jan-17 2:01 
GeneralRe: Alarm Manager Pin
Pavlex429-Jan-17 2:13
Pavlex429-Jan-17 2:13 
GeneralRe: Alarm Manager Pin
Richard MacCutchan29-Jan-17 2:18
mveRichard MacCutchan29-Jan-17 2:18 
GeneralRe: Alarm Manager Pin
Afzaal Ahmad Zeeshan29-Jan-17 2:22
professionalAfzaal Ahmad Zeeshan29-Jan-17 2:22 
GeneralRe: Alarm Manager Pin
Richard MacCutchan29-Jan-17 2:47
mveRichard MacCutchan29-Jan-17 2:47 
GeneralRe: Alarm Manager Pin
Afzaal Ahmad Zeeshan29-Jan-17 3:07
professionalAfzaal Ahmad Zeeshan29-Jan-17 3:07 
AnswerRe: Alarm Manager Pin
Afzaal Ahmad Zeeshan29-Jan-17 2:20
professionalAfzaal Ahmad Zeeshan29-Jan-17 2:20 
GeneralRe: Alarm Manager Pin
Pavlex429-Jan-17 3:40
Pavlex429-Jan-17 3:40 
AnswerRe: Alarm Manager Pin
Afzaal Ahmad Zeeshan29-Jan-17 3:56
professionalAfzaal Ahmad Zeeshan29-Jan-17 3:56 
GeneralRe: Alarm Manager Pin
Pavlex429-Jan-17 4:00
Pavlex429-Jan-17 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.