GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — add intentService demo · fuzi4it/Android_Blog_Demos@3ed6e1e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • blogcodes/src/main
    • AndroidManifest.xml
    • java/com/zhy/blogcodes
      • CategoryActivity.java
      • intentservice
        • HandlerThreadActivity.java
        • IntentServiceActivity.java
        • UploadImgService.java
    • res
      • layout
        • activity_intent_service.xml
        • activity_thread_handler.xml
      • menu
        • menu_intent.xml
        • menu_thread_handler.xml
      • values
        • strings.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎blogcodes/src/main/AndroidManifest.xml‎

Copy file name to clipboardExpand all lines: blogcodes/src/main/AndroidManifest.xml
+29-14Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,65 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest package="com.zhy.blogcodes"
3-
xmlns:android="http://schemas.android.com/apk/res/android">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.zhy.blogcodes" >
44

5-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6+
<uses-permission android:name="android.permission.INTERNET" />
67

78
<application
89
android:allowBackup="true"
910
android:icon="@mipmap/ic_launcher"
1011
android:label="@string/app_name"
11-
android:theme="@style/AppTheme">
12+
android:theme="@style/AppTheme" >
1213
<activity
1314
android:name=".CategoryActivity"
14-
android:label="@string/app_name">
15+
android:label="@string/app_name" >
1516
<intent-filter>
16-
<action android:name="android.intent.action.MAIN"/>
17+
<action android:name="android.intent.action.MAIN" />
1718

18-
<category android:name="android.intent.category.LAUNCHER"/>
19+
<category android:name="android.intent.category.LAUNCHER" />
1920
</intent-filter>
2021
</activity>
2122
<activity
2223
android:name=".nav.NavigationViewActivity"
2324
android:label="@string/title_activity_category"
24-
android:theme="@style/Theme.DesignDemo">
25+
android:theme="@style/Theme.DesignDemo" >
2526
</activity>
2627
<activity
2728
android:name=".nav.NavListViewActivity"
2829
android:label="@string/title_activity_base_content"
29-
android:theme="@style/Theme.DesignDemo">
30+
android:theme="@style/Theme.DesignDemo" >
3031
</activity>
31-
<activity android:name=".vdh.VDHBlogActivity"></activity>
32+
<activity android:name=".vdh.VDHBlogActivity" />
3233
<activity
3334
android:name=".mvp.UserLoginActivity"
34-
android:label="@string/title_activity_user_login">
35+
android:label="@string/title_activity_user_login" >
3536
</activity>
3637
<activity
3738
android:name=".dl.DLCategoryActivity"
38-
android:label="@string/title_activity_dlactivity01">
39+
android:label="@string/title_activity_dlactivity01" >
3940
</activity>
4041
<activity
4142
android:name=".dl.DLActivity01"
42-
android:label="@string/title_activity_dlactivity01">
43+
android:label="@string/title_activity_dlactivity01" >
4344
</activity>
4445
<activity
4546
android:name=".dl.DLProxyActivity"
4647
android:label="@string/title_activity_dlbase"
47-
android:theme="@android:style/Theme.Light">
48+
android:theme="@android:style/Theme.Light" >
49+
</activity>
50+
<activity
51+
android:name=".intentservice.HandlerThreadActivity"
52+
android:label="@string/title_activity_thread_handler" >
53+
</activity>
54+
55+
<service
56+
android:name=".intentservice.UploadImgService"
57+
android:exported="false" >
58+
</service>
59+
60+
<activity
61+
android:name=".intentservice.IntentServiceActivity"
62+
android:label="@string/title_activity_intent_service" >
4863
</activity>
4964
</application>
5065

Collapse file

‎blogcodes/src/main/java/com/zhy/blogcodes/CategoryActivity.java‎

Copy file name to clipboardExpand all lines: blogcodes/src/main/java/com/zhy/blogcodes/CategoryActivity.java
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import android.widget.ListView;
1212
import android.widget.TextView;
1313

14-
import com.zhy.blogcodes.dl.DLCategoryActivity;
14+
import com.zhy.blogcodes.intentservice.HandlerThreadActivity;
15+
import com.zhy.blogcodes.intentservice.IntentServiceActivity;
1516
import com.zhy.blogcodes.mvp.UserLoginActivity;
1617
import com.zhy.blogcodes.nav.NavListViewActivity;
1718
import com.zhy.blogcodes.nav.NavigationViewActivity;
@@ -32,6 +33,8 @@ public class CategoryActivity extends ActionBarActivity
3233
UserLoginActivity.class,
3334
VDHBlogActivity.class,
3435
//DLCategoryActivity.class,
36+
HandlerThreadActivity.class,
37+
IntentServiceActivity.class,
3538

3639
};
3740

Collapse file

‎blogcodes/src/main/java/com/zhy/blogcodes/intentservice/HandlerThreadActivity.java‎

Copy file name to clipboard
+117Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.zhy.blogcodes.intentservice;
2+
3+
import android.os.Bundle;
4+
import android.os.Handler;
5+
import android.os.HandlerThread;
6+
import android.os.Message;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.text.Html;
9+
import android.widget.TextView;
10+
11+
import com.zhy.blogcodes.R;
12+
13+
14+
public class HandlerThreadActivity extends AppCompatActivity
15+
{
16+
17+
private TextView mTvServiceInfo;
18+
19+
private HandlerThread mCheckMsgThread;
20+
private Handler mCheckMsgHandler;
21+
private boolean isUpdateInfo;
22+
23+
private static final int MSG_UPDATE_INFO = 0x110;
24+
25+
//与UI线程管理的handler
26+
private Handler mHandler = new Handler();
27+
28+
29+
@Override
30+
protected void onCreate(Bundle savedInstanceState)
31+
{
32+
super.onCreate(savedInstanceState);
33+
setContentView(R.layout.activity_thread_handler);
34+
35+
//创建后台线程
36+
initBackThread();
37+
38+
mTvServiceInfo = (TextView) findViewById(R.id.id_textview);
39+
40+
}
41+
42+
@Override
43+
protected void onResume()
44+
{
45+
super.onResume();
46+
//开始查询
47+
isUpdateInfo = true;
48+
mCheckMsgHandler.sendEmptyMessage(MSG_UPDATE_INFO);
49+
}
50+
51+
@Override
52+
protected void onPause()
53+
{
54+
super.onPause();
55+
//停止查询
56+
isUpdateInfo = false;
57+
mCheckMsgHandler.removeMessages(MSG_UPDATE_INFO);
58+
59+
}
60+
61+
private void initBackThread()
62+
{
63+
mCheckMsgThread = new HandlerThread("check-message-coming");
64+
mCheckMsgThread.start();
65+
mCheckMsgHandler = new Handler(mCheckMsgThread.getLooper())
66+
{
67+
@Override
68+
public void handleMessage(Message msg)
69+
{
70+
checkForUpdate();
71+
if (isUpdateInfo)
72+
{
73+
mCheckMsgHandler.sendEmptyMessageDelayed(MSG_UPDATE_INFO, 1000);
74+
}
75+
}
76+
};
77+
78+
79+
}
80+
81+
/**
82+
* 模拟从服务器解析数据
83+
*/
84+
private void checkForUpdate()
85+
{
86+
try
87+
{
88+
//模拟耗时
89+
Thread.sleep(1000);
90+
mHandler.post(new Runnable()
91+
{
92+
@Override
93+
public void run()
94+
{
95+
String result = "实时更新中,当前大盘指数:<font color='red'>%d</font>";
96+
result = String.format(result, (int) (Math.random() * 3000 + 1000));
97+
mTvServiceInfo.setText(Html.fromHtml(result));
98+
}
99+
});
100+
101+
} catch (InterruptedException e)
102+
{
103+
e.printStackTrace();
104+
}
105+
106+
}
107+
108+
@Override
109+
protected void onDestroy()
110+
{
111+
super.onDestroy();
112+
//释放资源
113+
mCheckMsgThread.quit();
114+
}
115+
116+
117+
}
Collapse file

‎blogcodes/src/main/java/com/zhy/blogcodes/intentservice/IntentServiceActivity.java‎

Copy file name to clipboard
+111Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.zhy.blogcodes.intentservice;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.content.IntentFilter;
7+
import android.os.Bundle;
8+
import android.support.v7.app.AppCompatActivity;
9+
import android.view.Menu;
10+
import android.view.MenuItem;
11+
import android.view.View;
12+
import android.widget.LinearLayout;
13+
import android.widget.TextView;
14+
15+
import com.zhy.blogcodes.R;
16+
17+
public class IntentServiceActivity extends AppCompatActivity
18+
{
19+
20+
public static final String UPLOAD_RESULT = "com.zhy.blogcodes.intentservice.UPLOAD_RESULT";
21+
22+
private LinearLayout mLyTaskContainer;
23+
24+
private BroadcastReceiver uploadImgReceiver = new BroadcastReceiver()
25+
{
26+
@Override
27+
public void onReceive(Context context, Intent intent)
28+
{
29+
if (intent.getAction() == UPLOAD_RESULT)
30+
{
31+
String path = intent.getStringExtra(UploadImgService.EXTRA_IMG_PATH);
32+
33+
handleResult(path);
34+
35+
}
36+
37+
}
38+
};
39+
40+
private void handleResult(String path)
41+
{
42+
TextView tv = (TextView) mLyTaskContainer.findViewWithTag(path);
43+
tv.setText(path + " upload success ~~~ ");
44+
}
45+
46+
47+
@Override
48+
protected void onCreate(Bundle savedInstanceState)
49+
{
50+
super.onCreate(savedInstanceState);
51+
setContentView(R.layout.activity_intent_service);
52+
53+
mLyTaskContainer = (LinearLayout) findViewById(R.id.id_ll_taskcontainer);
54+
55+
registerReceiver();
56+
}
57+
58+
private void registerReceiver()
59+
{
60+
IntentFilter filter = new IntentFilter();
61+
filter.addAction(UPLOAD_RESULT);
62+
registerReceiver(uploadImgReceiver, filter);
63+
}
64+
65+
int i = 0;
66+
67+
public void addTask(View view)
68+
{
69+
//模拟路径
70+
String path = "/sdcard/imgs/" + (++i) + ".png";
71+
UploadImgService.startUploadImg(this, path);
72+
73+
TextView tv = new TextView(this);
74+
mLyTaskContainer.addView(tv);
75+
tv.setText(path + " is uploading ...");
76+
tv.setTag(path);
77+
}
78+
79+
80+
@Override
81+
protected void onDestroy()
82+
{
83+
super.onDestroy();
84+
unregisterReceiver(uploadImgReceiver);
85+
}
86+
87+
@Override
88+
public boolean onCreateOptionsMenu(Menu menu)
89+
{
90+
// Inflate the menu; this adds items to the action bar if it is present.
91+
getMenuInflater().inflate(R.menu.menu_intent, menu);
92+
return true;
93+
}
94+
95+
@Override
96+
public boolean onOptionsItemSelected(MenuItem item)
97+
{
98+
// Handle action bar item clicks here. The action bar will
99+
// automatically handle clicks on the Home/Up button, so long
100+
// as you specify a parent activity in AndroidManifest.xml.
101+
int id = item.getItemId();
102+
103+
//noinspection SimplifiableIfStatement
104+
if (id == R.id.action_settings)
105+
{
106+
return true;
107+
}
108+
109+
return super.onOptionsItemSelected(item);
110+
}
111+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.